官方文档截图
//注: 获取Excel文件(.xls和.xlsx都支持)
public static JSONArray excel2Json(File file) throws IllegalAccessError, IOException {
if (file == null) {
throw new NullPointerException("the file is null.");
} else if (file.getName().endsWith(XLS)) {
return XLS2Json(file);
} else if (file.getName().endsWith(XLSX)) {
return XLSX2Json(file);
}
throw new IllegalAccessError("the file[" + file.getName() + "] is not excel file.");
}
private static JSONArray XLS2Json(File file) throws IOException {
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(new FileInputStream(file));
Workbook book = new HSSFWorkbook(poifsFileSystem);
Sheet sheet = book.getSheetAt(0);
return read(sheet);
}
private static JSONArray XLSX2Json(File file) throws IOException {
Workbook book = new XSSFWorkbook(new FileInputStream(file));
Sheet sheet = book.getSheetAt(0);
return read(sheet);
}
1、maven依赖
org.apache.poi
poi-ooxml
3.10-FINAL
net.sf.json-lib
json-lib
2.4
jdk15
2、Jar包依赖
结尾dome中附带
或者官网下载最新 https://www.apache.org/dyn/closer.lua/poi/release/bin/poi-bin-3.17-20170915.zip
以下为我下载的解压截图
package org.hbm.excel;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 介绍: Excel与Json互转操作
* author:胡邦茂
* CreateDate: 2018/7/26 15:16
* 拓展:2007版本以前.xls ,2007版本以后 就有.xlsx格式的了,2010,包括2013版本都是 默认的都是xlsx格式
*/
public class ExcelTool {
private static final String XLS = ".xls";
private static final String XLSX = ".xlsx";
//注: 获取Excel文件(.xls和.xlsx都支持)
static JSONArray excel2Json(File file) throws IllegalAccessError, IOException {
if (file == null) {
throw new NullPointerException("the file is null.");
} else if (file.getName().endsWith(XLS)) {
return XLS2Json(file);
} else if (file.getName().endsWith(XLSX)) {
return XLSX2Json(file);
}
throw new IllegalAccessError("the file[" + file.getName() + "] is not excel file.");
}
private static JSONArray XLS2Json(File file) throws IOException {
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(new FileInputStream(file));
Workbook book = new HSSFWorkbook(poifsFileSystem);
Sheet sheet = book.getSheetAt(0);
return read(sheet);
}
private static JSONArray XLSX2Json(File file) throws IOException {
Workbook book = new XSSFWorkbook(new FileInputStream(file));
Sheet sheet = book.getSheetAt(0);
return read(sheet);
}
//注:读取Excel转为Json
private static JSONArray read(Sheet sheet) {
// 首行下标
int rowStart = sheet.getFirstRowNum();
// 尾行下标
int rowEnd = sheet.getLastRowNum();
// 如果首行与尾行相同,表明只有一行,直接返回空数组
if (rowStart == rowEnd) {
return new JSONArray();
}
Row firstRow = sheet.getRow(rowStart);
//开始单元格下标
int cellStart = firstRow.getFirstCellNum();
//一排结束单元格下标
int cellEnd = firstRow.getLastCellNum();
Map keyMap = new HashMap<>();
for (int i = cellStart; i < cellEnd; i++) {
keyMap.put(i, getValue(firstRow.getCell(i), rowStart, i, true));
}
// 获取每行JSON对象的值
JSONArray array = new JSONArray();
for (int i = rowStart + 1; i <= rowEnd; i++) {
Row eachRow = sheet.getRow(i);
JSONObject obj = new JSONObject();
StringBuilder sb = new StringBuilder();
for (int k = cellStart; k < cellEnd; k++) {
if (eachRow != null) {
String val = getValue(eachRow.getCell(k), i, k, false);
sb.append(val);
obj.put(keyMap.get(k), val);
}
}
//改行有内容
if (sb.toString().length() > 0) {
array.add(obj);
}
sb.delete(0, sb.length());
}
return array;
}
/**
* 获取每个单元格的数据
*
* @param cell 单元格对象
* @param rowNum 第几行
* @param index 该行第几个
* @param isKey 是否为键:true-是,false-不是。 如果解析Json键,值为空时报错;如果不是Json键,值为空不报错
*/
private static String getValue(Cell cell, int rowNum, int index, boolean isKey) {
// 空白或空
if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
if (isKey) {
throw new NullPointerException(String.format("the key on row %s index %s is null ", ++rowNum, ++index));
} else {
return "";
}
}
// 0. 数字 类型
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(date);
}
String val = cell.getNumericCellValue() + "";
val = val.toUpperCase();
if (val.contains("E")) {
val = val.split("E")[0].replace(".", "");
}
return val;
}
// 1. String类型
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
String val = cell.getStringCellValue();
if (val == null || val.trim().length() == 0) {
return "";
}
return val.trim();
}
// 2. 公式 CELL_TYPE_FORMULA
if (cell.getCellType() == Cell.CELL_TYPE_FORMULA) {
return cell.getStringCellValue();
}
// 4. 布尔值 CELL_TYPE_BOOLEAN
if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
return cell.getBooleanCellValue() + "";
}
// 5. 错误 CELL_TYPE_ERROR
return "";
}
//注:json转Excel
static File json2Excel(JSONArray array, File outPath) throws IOException {
if (array == null || outPath == null) {
throw new RuntimeException(array == null ? " jsonArray is null " : "outPath is Null");
}
if (outPath.getAbsolutePath().endsWith(XLS)) {
return json2XLS(array, outPath);
} else if (outPath.getAbsolutePath().endsWith(XLSX)) {
return json2XLSX(array, outPath);
} else {
throw new IllegalAccessError("the file [" + outPath.getName() + "] is not excel file.");
}
}
/**
* json导出Excel
* 注:旧版Excel 列最大支持 65536(含表头) 解决:可增加 Sheet
*/
private static File json2XLS(JSONArray array, File outPath) throws IOException {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet0");
HSSFRow row = sheet.createRow(0);
JSONObject headJson = array.getJSONObject(0);
int rowNum = 0;
//1、创建表头
for (Object s : headJson.keySet()) {
HSSFCell cell = row.createCell(rowNum++);
cell.setCellValue(String.valueOf(s));
}
//2、创建表内容
for (int i = 0; i < array.size(); i++) {
//新创建一行
row = sheet.createRow(i + 1);
JSONObject jsonObject = array.getJSONObject(i);
rowNum = 0;
for (Object s : jsonObject.keySet()) {
//行中的格子 (从左到右)
HSSFCell cell = row.createCell(rowNum++);
cell.setCellValue(jsonObject.getString((String) s));
}
}
//3、输出Excel文件
FileOutputStream fos = new FileOutputStream(outPath);
wb.write(fos);
fos.flush();
fos.close();
return outPath;
}
/**
* json导出Excel
* 注:测试创建 60万条没问题
*/
private static File json2XLSX(JSONArray array, File outPath) throws IOException {
XSSFWorkbook wb = new XSSFWorkbook();
XSSFSheet sheet = wb.createSheet("Sheet0");
int rowNum = 0;
XSSFRow row = sheet.createRow(0);
JSONObject headJson = array.getJSONObject(0);
//1、表头
for (Object s : headJson.keySet()) {
XSSFCell cell = row.createCell(rowNum++);
cell.setCellValue(String.valueOf(s));
}
//2、表内容
for (int i = 0; i < array.size(); i++) {
//新创建一行
row = sheet.createRow(i + 1);
JSONObject jsonObject = array.getJSONObject(i);
rowNum = 0;
for (Object s : jsonObject.keySet()) {
//行中的格子 (从左到右)
XSSFCell cell = row.createCell(rowNum++);
cell.setCellValue(jsonObject.getString((String) s));
}
}
//3、输出Excel文件
FileOutputStream fos = new FileOutputStream(outPath);
wb.write(fos);
fos.flush();
fos.close();
return outPath;
}
}