apache poi 读取xlsx并导出为json(没考虑xls)
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)的更多相关文章
- 用 Apache POI 读取 XLSX 数据
最近因为项目的原因,需要从一些 Microsoft Office Excel 文件读取数据并加载到数据库. Google了一下方法,发现其实可以用的 Java 第三方库很多,最著名的是 Apache ...
- Java开发小技巧(六):使用Apache POI读取Excel
前言 在数据仓库中,ETL最基础的步骤就是从数据源抽取所需的数据,这里所说的数据源并非仅仅是指数据库,还包括excel.csv.xml等各种类型的数据接口文件,而这些文件中的数据不一定是结构化存储的, ...
- 项目一:第四天 1、快递员的条件分页查询-noSession,条件查询 2、快递员删除(逻辑删除) 3、基于Apache POI实现批量导入区域数据 a)Jquery OCUpload上传文件插件使用 b)Apache POI读取excel文件数据
1. 快递员的条件分页查询-noSession,条件查询 2. 快递员删除(逻辑删除) 3. 基于Apache POI实现批量导入区域数据 a) Jquery OCUpload上传文件插件使用 b) ...
- Java 使用Apache POI读取和写入Excel表格
1,引入所用的包 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxm ...
- apache POI 操作excel<导入导出>
1.首先导入maven依赖 <!-- POI核心依赖 --> <dependency> <groupId>org.apache.poi</groupId> ...
- 使用poi读取xlsx中的数据
excel中的内容见下图: 详细代码: package dataprovider; import java.io.FileInputStream; import java.io.InputStream ...
- java使用org.apache.poi读取与保存EXCEL文件
一.读EXCEL文件 package com.ruijie.wis.cloud.utils; import java.io.FileInputStream; import java.io.FileNo ...
- POI读取excel工具类 返回实体bean集合(xls,xlsx通用)
本文举个简单的实例 读取上图的 excel文件到 List<User>集合 首先 导入POi 相关 jar包 在pom.xml 加入 <!-- poi --> <depe ...
- poi读取xlsx
知道 大家都知道用poi读取xls 当时有时候 必需要读取xlsx 如今我把我做測试的demo分享给大家 package com.lt.main; import java.io.File; imp ...
随机推荐
- 20175209 《Java程序设计》第七周学习总结
20175209 <Java程序设计>第七周学习总结 一.教材知识点总结 第八章 常用类和实用类 1.String类 构造String对象 常量对象:""中的字符序列, ...
- C# webapi 上传下载图片
客户端上传文件 string url = url + "webUploadFile"; Uri server = new Uri(url); HttpClient httpClie ...
- 使Python走向Effective系列目录
Effective以一词,并不单单局限于执行速度层面的高效率,同时有着令代码易于阅读.易于测试且易于维护等意思,此外,它还蕴藏着易于扩展.易于修改和易于多人协作等更为高阶的理念.如果能够通过一些具体的 ...
- Jenkins Sonar
sonar简介 SonarQube是 一个开源的代码质量分析平台,便于管理代码的质量,可检查出项目代码的漏洞和潜在的逻辑问题.同时,它提供了丰富的插件,支持多种语言的检测, 如 Java.Python ...
- 03-oracle中的高级查询
1.连接查询 1.1 使用连接谓词指定的连接 介绍: 在连接谓词表示形式中,连接条件由比较运算符在WHERE子句中给出,将这种表示形式称为连接谓词表示形式,连接谓词又称为连接条件. 语法: [< ...
- Angular记录(10)
文档资料 速查表:https://www.angular.cn/guide/cheatsheet 风格指南:https://www.angular.cn/guide/styleguide Angula ...
- maven配置阿里镜像仓库
打开maven的配置文件(windows机器一般在maven安装目录的conf/settings.xml),在<mirrors></mirrors>标签中添加mirror子节点 ...
- 开源顶级持久层框架——mybatis(ibatis)——day02
mybatis第二天 高级映射 查询缓存 和spring整合 课程复习: mybatis是什么? mybatis是一个持久层框架,mybatis ...
- vue项目打包笔记
我的需求是在同一个代码目录下,可以同时放入两个项目包,通过运行不同的命令,运行相应的项目页面以及打包相应的项目. 这样的话,代码管理比较方便,用于多个项目在同一时间开发,类型一样,但在功能上有所区分的 ...
- day 25-1 接口类、抽象类、多态
# 接口类:python 原生不支持# 抽象类:python 原生支持的 接口类 首先我们来看一个支付接口的简单例子 from abc import abstractmethod,ABCMeta #我 ...