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. python@wraps实现原理

    @wraps作用 python中的装饰器装饰过的函数其实就不是函数本身了,我们可以看看下面的例子 import time def timmer(func): """tim ...

  2. 【Unity】透明度渐变

    写给美术大佬的脚本,还要继续改,github地址:TransEffect[github] 效果图如下: Ver.1源码,针对3d Object: using System.Collections; u ...

  3. :focus-within伪类选择器的3种应用

    1.点击form里任意input隐藏form以外内容提高用户体验 <form><input type="text"></form> form { ...

  4. R-----shiny包的部分解释和控件介绍

    R-----shiny包的部分解释和控件介绍 作者:周彦通.贾慧 shinyApp( ui = fixedPage( fixedPanel( top = 50, right=50, width=200 ...

  5. c语言编译四大步

    -o: 指定生成后的文件名,后面跟指定的名称 四步:-E 预处理 > -S 编译 > -c 汇编 > 链接 -E: 表示预处理,生成文件为.i,会做宏(define)定义的展开.头文 ...

  6. dsp实验一 常见问题教程

    1.选择合适阅读材料:卖油翁.将进酒,等. 2.录音设备三选一:某种电脑声卡+线上convert/楼月mp3录音软件+格式工厂(我选的)/Matlab (注:这是目前已知的方法,我只用了第二个) 3. ...

  7. 「IOI2018」狼人

    快咕一个月了 咕咕咕 咕咕咕咕 LOJ #2865 Luogu P4899(离线) UOJ #407(强制在线) 题意 给定一棵树和若干组询问$(S,E,L,R)$ 表示你初始在$S$,想到达$E$, ...

  8. $HOME

    Ubuntu中 命令行输入$HOME bash: /home/XXX: Is a directory $HOME 是用户主目录

  9. ffmpeg错误:Invalid UE golomb code

    解决方案在github https://github.com/FFmpeg/FFmpeg/commit/c51c08e0e70c186971385bdbb225f69edd4e3375 问题简要描述为 ...

  10. windows环境安装phantomjs和pyspider遇到的问题

    1. 安装phantomjs 下载地址:http://phantomjs.org/download.html 解压后将phantomjs.exe文件放到python根目录 2.安装pyspider p ...