1、用到的jar包:fastjson-1.2.9、poi(poi-3.15、poi-ooxml-3.15、poi-ooxml-schemas-3.15、xmlbeans-2.6.0、commons-collections4-4.1)

很简单,直接上代码:

2、导出类,两个WrapAll类字符串数组都是excel文件名,如item.xlsx,写死的读取sheet 第 0 页

ParseJson方法导出为json,list是行,Map key-value:字段名-值

 package com.ojcgame.warp;

 import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress; import com.alibaba.fastjson.JSON;
import com.ojcgame.common.EnvironmentManager;
import com.ojcgame.common.OJCUtils; public class WarpDataManager {
String[] filesArr; public void WarpAll(String[] files) {
filesArr = files;
ProgressMonitorDialog progress = new ProgressMonitorDialog(null);
IRunnableWithProgress progressTask = new IRunnableWithProgress() {
@Override
public void run(IProgressMonitor monitor)
throws InvocationTargetException, InterruptedException {
monitor.beginTask("正在导出数据", IProgressMonitor.UNKNOWN);
WarpAll(filesArr, monitor);
}
}; try {
progress.run(true, false, progressTask);
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
filesArr = null;
}
} @SuppressWarnings("deprecation")
private void WarpAll(String[] files, IProgressMonitor monitor) {
InputStream is = null;
XSSFWorkbook xssfWorkbook = null;
List<String> titles = null;
Map<String, Object> oneCellData = null;
List<Map<String, Object>> AllDataList = null;
int fileIndex = 0;
try {
for (int f = 0, fLength = files.length; f < fLength; ++f) {
fileIndex = f;
// System.out.println("正在尝试导出:" + files[f]);
monitor.subTask("尝试导出:" + files[f]);
is = new FileInputStream(EnvironmentManager.getInstance()
.getDataSourcesFloderPath() + "\\" + files[f]);
xssfWorkbook = new XSSFWorkbook(is);
// 读取sheet1
XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
if (xssfSheet == null)
continue; titles = new ArrayList<String>();
AllDataList = new ArrayList<Map<String, Object>>();
// 先读取字段
XSSFRow titleRow = xssfSheet.getRow(0);
for (int rowIndex = 0, mLength = titleRow.getLastCellNum() + 1; rowIndex < mLength; ++rowIndex) {
if (null == titleRow.getCell(rowIndex)
|| titleRow.getCell(rowIndex).getCellType() == HSSFCell.CELL_TYPE_BLANK) {
break;
} else {
try {
// System.out.println(titles.get(cellNum) + "---"
// + xssfCell.getStringCellValue());
titles.add(titleRow.getCell(rowIndex)
.getStringCellValue());
} catch (IllegalStateException e) {
// System.out.println("rowIndex number:" + rowIndex
// + " ---- " + files[f]);
// System.out.println(titles.get(cellNum) + "---"
// + xssfCell.getNumericCellValue());
titles.add(titleRow.getCell(rowIndex)
.getNumericCellValue() + "");
}
}
}
// System.out.println(xssfSheet
// .getLastRowNum() + 1);
// 读取行
for (int rowNum = 2, rLength = xssfSheet.getLastRowNum() + 1; rowNum < rLength; ++rowNum) {
XSSFRow xssfRow = xssfSheet.getRow(rowNum);
if (xssfRow == null) {
continue;
}
oneCellData = new HashMap<String, Object>();
// 读取列
for (int cellNum = 0; cellNum < titles.size(); ++cellNum) {
XSSFCell xssfCell = xssfRow.getCell(cellNum);
if (null == xssfCell)
continue; if (xssfCell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {
// System.out.println(titles.get(cellNum) + "---"
// + xssfCell.getNumericCellValue());
oneCellData.put(titles.get(cellNum),
xssfCell.getNumericCellValue());
} else if (xssfCell.getCellType() == HSSFCell.CELL_TYPE_STRING) {
// System.out.println(titles.get(cellNum) + "---"
// + xssfCell.getStringCellValue());
oneCellData.put(titles.get(cellNum),
xssfCell.getStringCellValue());
} else if (xssfCell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
// System.out.println(cellNum + "--- kong=======" +
// rowNum);
// System.out
// .println(titles.get(cellNum) + "--- kong");
oneCellData.put(titles.get(cellNum), "");
} else if (xssfCell.getCellType() == HSSFCell.CELL_TYPE_FORMULA) {
try {
// System.out.println(titles.get(cellNum) +
// "---"
// + xssfCell.getStringCellValue());
oneCellData.put(titles.get(cellNum),
xssfCell.getStringCellValue());
} catch (IllegalStateException e) {
// System.out.println(titles.get(cellNum) +
// "---"
// + xssfCell.getNumericCellValue());
oneCellData.put(titles.get(cellNum),
xssfCell.getNumericCellValue());
}
}
} AllDataList.add(oneCellData);
} if (null != xssfWorkbook)
xssfWorkbook.close();
if (null != is)
is.close(); ParseJson(AllDataList, OJCUtils.GetFileName(files[f], ".xlsx")); monitor.worked(f + 1);
} } catch (Exception e) {
e.printStackTrace();
OJCUtils.ShowDialog("导出失败:" + files[fileIndex]);
} finally {
monitor.done();
try {
if (null != xssfWorkbook)
xssfWorkbook.close();
} catch (IOException e) {
e.printStackTrace();
OJCUtils.ShowDialog("导出失败:" + files[fileIndex]);
} finally {
try {
if (null != is)
is.close();
} catch (Exception e) {
e.printStackTrace();
OJCUtils.ShowDialog("导出失败:" + files[fileIndex]);
}
}
}
} private void ParseJson(List<Map<String, Object>> pContents, String pFileName) {
String jsonStr = JSON.toJSONString(pContents, true);
if (null == jsonStr || jsonStr.isEmpty()) {
return;
}
FileWriter writer = null;
try {
writer = new FileWriter(EnvironmentManager.getInstance()
.getDataTargetFloderPath() + "\\" + pFileName + ".json");
writer.write(jsonStr);
writer.flush();
} catch (Exception e) {
e.printStackTrace();
OJCUtils.ShowDialog("导出JSON失败:" + pFileName);
} finally {
try {
if (null != writer) {
writer.flush();
writer.close();
}
} catch (Exception e) {
e.printStackTrace();
OJCUtils.ShowDialog("导出JSON失败:" + pFileName);
}
}
} // public static void main(String[] args) {
// ProgressMonitorDialog progress = new ProgressMonitorDialog(null);
// IRunnableWithProgress progressTask = new IRunnableWithProgress() {
// @Override
// public void run(IProgressMonitor monitor)
// throws InvocationTargetException, InterruptedException {
// monitor.beginTask("正在导出数据", IProgressMonitor.UNKNOWN);
// WarpDataManager wdMgr = new WarpDataManager();
// wdMgr.WarpAll(new String[] { "skill.xlsx" }, monitor);
// monitor.done();
// }
// };
//
// try {
// progress.run(true, false, progressTask);
// } catch (InvocationTargetException e) {
// e.printStackTrace();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
}

apache poi 读取xlsx并导出为json(没考虑xls)的更多相关文章

  1. 用 Apache POI 读取 XLSX 数据

    最近因为项目的原因,需要从一些 Microsoft Office Excel 文件读取数据并加载到数据库. Google了一下方法,发现其实可以用的 Java 第三方库很多,最著名的是 Apache ...

  2. Java开发小技巧(六):使用Apache POI读取Excel

    前言 在数据仓库中,ETL最基础的步骤就是从数据源抽取所需的数据,这里所说的数据源并非仅仅是指数据库,还包括excel.csv.xml等各种类型的数据接口文件,而这些文件中的数据不一定是结构化存储的, ...

  3. 项目一:第四天 1、快递员的条件分页查询-noSession,条件查询 2、快递员删除(逻辑删除) 3、基于Apache POI实现批量导入区域数据 a)Jquery OCUpload上传文件插件使用 b)Apache POI读取excel文件数据

    1. 快递员的条件分页查询-noSession,条件查询 2. 快递员删除(逻辑删除) 3. 基于Apache POI实现批量导入区域数据 a) Jquery OCUpload上传文件插件使用 b) ...

  4. Java 使用Apache POI读取和写入Excel表格

    1,引入所用的包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxm ...

  5. apache POI 操作excel<导入导出>

    1.首先导入maven依赖 <!-- POI核心依赖 --> <dependency> <groupId>org.apache.poi</groupId> ...

  6. 使用poi读取xlsx中的数据

    excel中的内容见下图: 详细代码: package dataprovider; import java.io.FileInputStream; import java.io.InputStream ...

  7. java使用org.apache.poi读取与保存EXCEL文件

    一.读EXCEL文件 package com.ruijie.wis.cloud.utils; import java.io.FileInputStream; import java.io.FileNo ...

  8. POI读取excel工具类 返回实体bean集合(xls,xlsx通用)

    本文举个简单的实例 读取上图的 excel文件到 List<User>集合 首先 导入POi 相关 jar包 在pom.xml 加入 <!-- poi --> <depe ...

  9. poi读取xlsx

    知道 大家都知道用poi读取xls  当时有时候 必需要读取xlsx  如今我把我做測试的demo分享给大家 package com.lt.main; import java.io.File; imp ...

随机推荐

  1. 使用Excel VBA编程将网点的百度坐标转换后标注到高德地图上

    公司网点表存储的坐标是百度坐标,现需要将网点位置标注到高德地图上,研究了一下高德地图的云图数据模版 http://lbs.amap.com/yuntu/reference/cloudstorage和坐 ...

  2. C++:普通变量C++命名规则

    C++提倡使用拥有一定意义的变量名,使程序代码更有阅读性,命名是必须使用的几种简单的C++命名规则: 命名时只能使用:字母字符.数字和下划线(_); 第一个字符不能是数字: 区分大小写(C++对大小写 ...

  3. PTA编译总结求最大值及其下标

    代码: #include<stdio.h>    int main(void)    {    int i,index=0,n;    int a[10];    scanf(" ...

  4. docker容器网络

    1.我们在使用docker run创建Docker容器时,可以用--net选项指定容器的网络模式,Docker有以下4种网络模式: · host模式,使用--net=host指定 · containe ...

  5. Apicloud学习第四天

    apicloud存储机制,添加和获取存储的数据 $api.setStorage('currentCity', cityList[i_]); $api.getStorage('currentCity') ...

  6. Vue打包优化之分析工具webpack-bundle-analyzer

    // 1. 安装 cnpm install webpack-bundle-analyzer --save-dev // 2. 在/build/webpack.prod.conf.js文件中引入 con ...

  7. 用户认证授权和Shiro入门

    1.权限管理基础(认证和授权): 前言 本文主要讲解的知识点有以下: 权限管理的基础知识 模型 粗粒度和细粒度的概念 回顾URL拦截的实现 Shiro的介绍与简单入门 一.Shiro基础知识 在学习S ...

  8. 最短路径(Dijkstra算法)

    算法局限性:边的权值不能为负. 需要两个辅助数组dist[],path[],分别记录起点到各点的最短距离和最短路径 算法步骤: 1.根据起点v0初始化dist[]和path[]数组. 2.在剩下的点中 ...

  9. Intelij idea 版本控制工具窗口

    1. 在菜单栏vcs中启用版本控制 2. 然后调出工具窗口,view-tools window-version control

  10. windows powershell上批量修改文件名称

    $i = Get-ChildItem -Path c:\pictures -Filter *.jpg | ForEach-Object { $extension = $_.Extension $new ...