Apache POI是一个用于读写Microsoft Office文件格式的Java库,可以用来读取或写入Excel文件。
使用步骤
1.导入坐标
<!--alibaba-fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.9.graal</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.26.1</version>
</dependency>
<!-- Excel处理 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
2.封装类
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class ExcelHelper {
//文件后缀
private final static String XLSX = ".xlsx";
private final static String XLS = ".xls";
/**
* json数据导出Excel
*
* @param json json数据
* @param filepath 文件路径
* @return true 导出成功
*/
public static boolean jsonToExcel(String json, String filepath) {
try {
JSONArray array = JSONArray.parseArray(json);
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
// 创建表头
Row header = sheet.createRow(0);
JSONObject title = array.getJSONObject(0);
for (String s : title.keySet()) {
Cell cell = header.createCell(header.getLastCellNum() < 0 ? 0 : header.getLastCellNum());
cell.setCellValue(s);
}
// 填充数据
for (int i = 0; i < array.size(); i++) {
JSONObject object = array.getJSONObject(i);
Row row = sheet.createRow(i + 1);
int colNum = 0;
for (String key : object.keySet()) {
Object value = object.get(key);
row.createCell(colNum).setCellValue(String.valueOf(value));
colNum++;
}
}
// 导出到文件
OutputStream outputStream = new FileOutputStream(filepath);
workbook.write(outputStream);
workbook.close();
return true;
} catch (IOException e) {
System.out.println(e.getMessage());
return false;
}
}
/**
* Excel数据读取成json
*
* @param filepath 文件路径
* @return json数据
*/
public static JSONArray excelToJsonArray(String filepath) throws IOException, InvalidFormatException {
File file = new File(filepath);
String flieName = file.getName();
Workbook book = null;
Sheet sheet = null;
if (flieName.endsWith(XLSX)) {
book = new XSSFWorkbook(file);
sheet = book.getSheetAt(0);
}
if (flieName.endsWith(XLS)) {
POIFSFileSystem poifsFileSystem = new POIFSFileSystem(new FileInputStream(file));
book = new HSSFWorkbook(poifsFileSystem);
sheet = book.getSheetAt(0);
}
if (sheet != null) {
return readSheet(sheet, book);
}
return new JSONArray();
}
/**
* 读取Excel
*
* @param sheet 工作表
* @param book 工作簿
* @return json数据
*/
public static JSONArray readSheet(Sheet sheet, Workbook book) throws IOException {
int rowStart = sheet.getFirstRowNum(); // 首行下标
int rowEnd = sheet.getLastRowNum(); // 尾行下标
// 如果首行与尾行相同,表明只有一行,直接返回空数组
if (rowStart == rowEnd) {
book.close();
return new JSONArray();
}
// 获取第一行JSON对象键
Row firstRow = sheet.getRow(rowStart);
int cellStart = firstRow.getFirstCellNum();
int cellEnd = firstRow.getLastCellNum();
Map<Integer, String> keyMap = new HashMap<Integer, String>();
for (int j = cellStart; j < cellEnd; j++) {
keyMap.put(j, getValue(firstRow.getCell(j), rowStart, j, book, true));
}
// 获取每行JSON对象的值
JSONArray array = new JSONArray();
for (int i = rowStart + 1; i <= rowEnd; i++) {
Row eachRow = sheet.getRow(i);
JSONObject obj = new JSONObject();
StringBuffer sb = new StringBuffer();
for (int k = cellStart; k < cellEnd; k++) {
if (eachRow != null) {
String val = getValue(eachRow.getCell(k), i, k, book, false);
sb.append(val); // 所有数据添加到里面,用于判断该行是否为空
obj.put(keyMap.get(k), val);
}
}
if (!sb.toString().isEmpty()) {
array.add(obj);
}
}
book.close();
return array;
}
/**
* 获取每个单元格的数据
*
* @param cell 单元格对象
* @param rowNum 第几行
* @param index 该行第几个
* @param book 主要用于关闭流
* @param isKey 是否为键:true-是,false-不是。 如果解析Json键,值为空时报错;如果不是Json键,值为空不报错
*/
public static String getValue(Cell cell, int rowNum, int index, Workbook book, boolean isKey) throws IOException {
// 空白或空
if (cell == null || cell.getCellTypeEnum() == CellType.BLANK) {
if (isKey) {
book.close();
throw new NullPointerException(String.format("the key on row %s index %s is null ", ++rowNum, ++index));
} else {
return "";
}
}
// 0. 数字 类型
if (cell.getCellTypeEnum() == CellType.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.getCellTypeEnum() == CellType.STRING) {
String val = cell.getStringCellValue();
if (val == null || val.trim().isEmpty()) {
if (book != null) {
book.close();
}
return "";
}
return val.trim();
}
// 2. 公式 CELL_TYPE_FORMULA
if (cell.getCellTypeEnum() == CellType.FORMULA) {
return cell.getStringCellValue();
}
// 4. 布尔值 CELL_TYPE_BOOLEAN
if (cell.getCellTypeEnum() == CellType.BOOLEAN) {
return cell.getBooleanCellValue() + "";
}
// 5. 错误 CELL_TYPE_ERROR
return "";
}
}
3.使用方式
public static void main(String[] args) throws IOException, InvalidFormatException {
//导出Excel
var json = "[{\"name\":\"张三\",\"age\":\"18\"},{\"name\":\"李四\",\"age\":\"19\"}]";
System.out.println(jsonToExcel(json, "D:\\test.xlsx"));
//读取Excel
System.out.println(excelToJsonArray("D:\\test.xlsx"));
}