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. 关于pyx文件的修改

    在项目中碰到了关于pyx文件的修改,记录一下,省的以后还得查 官网快速入门地址:https://cython.readthedocs.io/en/latest/src/tutorial/cython_ ...

  2. Mac 装机必备软件推荐

    所谓Mac 装机必备软件,就是用 Mac OS X 几乎都要安装的软件,无论你是开发者.设计师还是摄影师,如果你是刚开始用 Mac,那么推荐看看以下内容,对你非常有帮助. 一.输入法 Mac 自带的中 ...

  3. Jenkins_安装

    1.下载war包 wget -c -O ./jenkins.war http://mirrors.jenkins.io/war-stable/latest/jenkins.war 2.启动下载好的wa ...

  4. airflow 笔记

    首先是一个比较好的英文网站,可能要fq:http://site.clairvoyantsoft.com/installing-and-configuring-apache-airflow/ ===== ...

  5. ASP.Net获取Aras连接,并获取Innovator实例

    首先需要在自己的项目bin目录下引入Aras的dll(../Aras\Innovator\Innovator\Server\bin). 注意:在引入Aras的dll时.需要注意自己的操作系统的位数.因 ...

  6. ASP.NET后台调用API方法

    /// <summary> /// 调用API POST请求与获取结果 /// </summary> ///URL 与 JSON串 public static string H ...

  7. Debian Security Advisory DSA-4419-1 twig security update

    Package        : twigCVE ID         : CVE-2019-9942 Fabien Potencier discovered that twig, a templat ...

  8. Linux基础系统优化及常用命令

    # Linux基础系统优化及常用命令 [TOC] ## Linux基础系统优化 Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令,在配置服务器基础环境时,先了解下网络参数设定命令. - ...

  9. 关于HashSet集合add元素

    HashSet集合add元素底层实现使用的是HashMap. 简单记忆:无论HashMap put元素还是HashSet add元素,都先调用hashCode()方法,若hashCode方法返回值不同 ...

  10. Selenium for C#(一) 环境安装

    Selenium 环境安装 本地环境为VS2015,由于selenium 官网不知什么原因打不开. 特记录下VS上使用NuGet安装Selenium的步骤. 利用Package Manager Con ...