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. hyper发送表单数据

    前言 某个美丽的下午,运维把服务器上的nginx升级了,http协议也变成了http2.0,我本地的requests再也连接不到服务器,然后就找到了额hyper 但是hyper的文档写的很简单,而且相 ...

  2. Sublime Text3—自带快捷键介绍

    摘要: Sublime Text是个小巧便捷的编辑器,除了众多好用的插件外,还有它自带的快捷键,打代码事半功倍,不会用的赶紧看看吧! 其实菜单上都有,看不懂可以汉化,Key Bindings-Defa ...

  3. Java IO系列之四:NIO通信模型

    分布式rpc框架有很多,比如dubbo,netty,还有很多其他的产品.但他们大部分都是基于nio的, nio是非阻塞的io,那么它的内部机制是怎么实现的呢. 1.由一个专门的线程处理所有IO事件,并 ...

  4. jQuery使用(十五):when()方法的使用

    这个方法再次基于callbacks和deferred对象来实现的一个延迟异步回调来实现的,再when方法内可以添加多个deferred对象作为参数,只有当所有deferred全部被触发了成功的回调函数 ...

  5. day21双下方法,源码相关,异常处理

    #!/usr/bin/env python# -*- coding:utf-8 -*- # 1.列举你了解的面向对象中的特殊成员,并为每个写代码示例.'''__init__初始化:class A: d ...

  6. SHELL:多文件的重命名和移动

    rename find mv #实现查找png 和 jpeg文件 #!/bin/bash #file name: rename.sh #use: rename.jpg  and .png files ...

  7. 使用JQuery实现图片轮播效果

    [效果如图] [原理简述] 这里大概说一下整个流程: 1,将除了第一张以外的图片全部隐藏, 2,获取第一张图片的alt信息显示在信息栏,并添加点击事件 3,为4个按钮添加点击侦听,点击相应的按钮,用f ...

  8. Linux-Shell编程之数组操作

    源码 #!/bin/bash str="Array - Demo Shell"; echo ${#str} #求字符串長度 #定義 arr=('a' 'b' 'c' 'd' 'e' ...

  9. Flask-Login用户登陆

    Flask-Login Flask-Login 提供用户会话管理,处理常见的登录.退出和注册的任务. Flask-Login 没有绑定数据库,可以从数据库回调用户对象. 安装flask-login p ...

  10. go之路

    目录 go初识[第一篇]初识 go初识[第二篇]包.变量.函数