refactor(phase-7): Excel 栈收口到 POI 并移除 EasyExcel

xlsx 改走 OPCPackage/XSSFReader 事件模型(流式,不整表入内存),
xls 走 HSSF;对外 readFirstSheet/readAllSheets 契约与行回调不变。
解析失败统一收成 IOException,行处理异常按 BusinessException/RuntimeException 原样透传。
pom 删 EasyExcel 4.0.3,只保留 poi-ooxml 5.4.1,消除双 POI 版本劈叉。
This commit is contained in:
2026-10-24 22:31:51 +08:00
parent 969f86c612
commit bb4cf3cdd2
4 changed files with 327 additions and 95 deletions
@@ -1,16 +1,23 @@
package com.nanri.aiimage.common.excel;
import com.alibaba.excel.EasyExcel;
import com.nanri.aiimage.common.exception.BusinessException;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.eventusermodel.XSSFReader;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Iterator;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@@ -97,11 +104,42 @@ class ExcelStreamReaderParseFailureTest {
assertEquals("Nike", seen.toString());
}
private Path writeWorkbook() {
/** xls 分支单独钉:BIFF8 走 HSSFWorkbook,不能因为 xlsx 改事件模型就把旧格式丢了。 */
@Test
void hssfWorkbookStillReadsRows() throws Exception {
Path file = tempDir.resolve("ok.xls");
try (HSSFWorkbook workbook = new HSSFWorkbook()) {
Sheet sheet = workbook.createSheet("Sheet1");
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("品牌");
header.createCell(1).setCellValue("ASIN");
Row data = sheet.createRow(1);
data.createCell(0).setCellValue("Nike");
data.createCell(1).setCellValue("B01ABC");
try (OutputStream out = Files.newOutputStream(file)) {
workbook.write(out);
}
}
StringBuilder seen = new StringBuilder();
ExcelStreamReader.readFirstSheet(file.toFile(), (sheetName, sheetNo, rowIndex, headerMap, rowMap) ->
seen.append(rowMap.get(0)));
assertEquals("Nike", seen.toString());
}
private Path writeWorkbook() throws IOException {
Path file = tempDir.resolve("ok.xlsx");
List<List<String>> head = List.of(List.of("品牌"), List.of("ASIN"));
List<List<String>> data = List.of(List.of("Nike", "B01ABC"));
EasyExcel.write(file.toFile()).head(head).sheet("Sheet1").doWrite(data);
try (XSSFWorkbook workbook = new XSSFWorkbook()) {
Sheet sheet = workbook.createSheet("Sheet1");
Row header = sheet.createRow(0);
header.createCell(0).setCellValue("品牌");
header.createCell(1).setCellValue("ASIN");
Row data = sheet.createRow(1);
data.createCell(0).setCellValue("Nike");
data.createCell(1).setCellValue("B01ABC");
try (OutputStream out = Files.newOutputStream(file)) {
workbook.write(out);
}
}
return file;
}
@@ -126,17 +164,28 @@ class ExcelStreamReaderParseFailureTest {
/** 直接喂同一个坏包,取 POI 自己报出来的真因文本,用来断言它没被收口吞掉。 */
private String rawParseFailureMessageOf(Path badFile) throws IOException {
String message = null;
try (var input = Files.newInputStream(badFile)) {
EasyExcel.read(input).sheet(0).headRowNumber(1).doRead();
throw new AssertionError("构造的坏包必须让 EasyExcel 抛异常,否则这条用例证明不了收口");
} catch (RuntimeException ex) {
Throwable deepest = ex;
while (deepest.getCause() != null) {
deepest = deepest.getCause();
Throwable failure = null;
try (InputStream input = Files.newInputStream(badFile);
OPCPackage pkg = OPCPackage.open(input)) {
XSSFReader reader = new XSSFReader(pkg);
reader.getSharedStringsTable();
Iterator<InputStream> sheets = reader.getSheetsData();
if (sheets.hasNext()) {
try (InputStream sheet = sheets.next()) {
sheet.transferTo(OutputStream.nullOutputStream());
}
}
message = deepest.getMessage();
throw new AssertionError("构造的坏包必须让 POI 抛异常,否则这条用例证明不了收口");
} catch (AssertionError error) {
throw error;
} catch (Exception ex) {
failure = ex;
}
Throwable deepest = failure;
while (deepest.getCause() != null) {
deepest = deepest.getCause();
}
String message = deepest.getMessage();
assertTrue(message != null && message.length() > 20,
"底层真因文本应当有信息量,实际=" + message);
return message;