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:
+64
-15
@@ -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;
|
||||
|
||||
@@ -94,8 +94,18 @@ class CatchSilenceRatchetTest {
|
||||
* {@code ba20f5ed feat(workbench-client)} 那枚新增 catch,不属本批、也未由本批记名**,
|
||||
* 该由那一笔自己把出处补进这份 diff。每一处本批的新增都在这里点名。
|
||||
* 不重抛/只 log/空体三格上限一个字没动。
|
||||
*
|
||||
* <p>10-24 Phase 7 EasyExcel→POI 收口再 +4,逐处点名(全部是 RETHROW 形状,气味三格未动):
|
||||
* <ul>
|
||||
* <li>{@code ExcelStreamReader.readOoxml} 三枚:{@code OpenXML4JException}(坏包)、
|
||||
* {@code SAXException}(sheet XML 坏了)、{@code RuntimeException}(POI 解析期异常)
|
||||
* 全部经 {@code toContractException} 转成 IOException 或原样重抛,不吞;</li>
|
||||
* <li>{@code ExcelStreamReader.readHssf} 一枚:{@code RuntimeException} 同上收口。</li>
|
||||
* </ul>
|
||||
* 这 4 处是替换 EasyExcel 监听器后的等价出口(原实现的 catch 只在 {@code runRead} 一处,
|
||||
* 但监听器内部另有 POI 的异常分诊);实测 total 1008。
|
||||
*/
|
||||
static final int STRICT_TOTAL = 1004;
|
||||
static final int STRICT_TOTAL = 1008;
|
||||
/**
|
||||
* 重抛型 catch 的**下限**(不是上限):本轮把两处吞异常改成重抛之后实测 388。
|
||||
* 这一格只能涨不能跌——它是"异常会往外走"的总量,把它钉成"只许减"就等于拦着别人修吞异常。
|
||||
|
||||
Reference in New Issue
Block a user