refactor: 第二批收敛——公共 Excel 写出器 + 任务收尾零件下沉 + 前端上传区/任务面板抽取

This commit is contained in:
2026-10-01 15:00:52 +08:00
parent 4b29a150f3
commit fa4bd57016
32 changed files with 2467 additions and 2826 deletions
@@ -0,0 +1,135 @@
package com.nanri.aiimage.common.util;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 公共 SXSSF 流式写出器:统一 {@code new SXSSFWorkbook(200) + setCompressTempFiles(true)}
* + write + try/finally {@code close()+dispose()} 骨架,并提供空值安全的单元格写入与列宽换算。
*
* <p>此前 8 个 {@code *ExcelAssemblyService} 与独立写出实现各自复制这段骨架和
* {@code value == null ? "" : value}、{@code n * 256} 样板;本类把骨架收敛到一处,
* 表头定义与行映射仍保留在各装配类中(调用方持有 Sheet/Row,本类只提供工作簿生命周期
* 与零件方法),因此日期/数字/特殊样式等模块语义不会被偷偷改掉。
*
* <p>用法:
* <pre>{@code
* try (SxssfWorkbookWriter writer = SxssfWorkbookWriter.open()) {
* Sheet sheet = writer.createSheet("结果", HEADER, new int[]{20, 18, 18});
* for (RowDto item : rows) {
* Row row = sheet.createRow(rowIndex++);
* SxssfWorkbookWriter.setText(row, 0, item.getAsin());
* }
* writer.write(outputXlsx);
* }
* }</pre>
*/
public final class SxssfWorkbookWriter implements AutoCloseable {
/** 各模块历史实现统一的滚动窗口大小(ShopDataCrawl 流式路径的自定义窗口除外)。 */
public static final int DEFAULT_ROW_ACCESS_WINDOW = 200;
private final SXSSFWorkbook workbook;
private CellStyle headerStyle;
private SxssfWorkbookWriter(int rowAccessWindow) {
this.workbook = new SXSSFWorkbook(rowAccessWindow);
this.workbook.setCompressTempFiles(true);
}
public static SxssfWorkbookWriter open() {
return new SxssfWorkbookWriter(DEFAULT_ROW_ACCESS_WINDOW);
}
public static SxssfWorkbookWriter open(int rowAccessWindow) {
return new SxssfWorkbookWriter(rowAccessWindow);
}
public SXSSFWorkbook workbook() {
return workbook;
}
/** 建表 + 第 0 行表头 + 列宽(单位:字符,内部换算 n*256)。columnWidthChars 为 null 时不设列宽。 */
public Sheet createSheet(String sheetName, String[] headers, int[] columnWidthChars) {
Sheet sheet = workbook.createSheet(sheetName);
writeHeaderRow(sheet, headers);
if (columnWidthChars != null) {
applyColumnWidths(sheet, columnWidthChars);
}
return sheet;
}
/** 在 sheet 第 0 行写入表头文字(不做样式;需要表头样式的调用方再逐格 setCellStyle)。 */
public void writeHeaderRow(Sheet sheet, String[] headers) {
Row headerRow = sheet.createRow(0);
for (int i = 0; i < headers.length; i++) {
headerRow.createCell(i).setCellValue(headers[i]);
}
}
/** 加粗表头样式(与 Publish/SimilarAsin 历史实现一致:bold Font + 空 CellStyle),惰性创建并复用。 */
public CellStyle headerStyle() {
if (headerStyle == null) {
Font font = workbook.createFont();
font.setBold(true);
CellStyle style = workbook.createCellStyle();
style.setFont(font);
headerStyle = style;
}
return headerStyle;
}
/** 写出文件(内部开 FileOutputStream)。失败时抛 IOException,工作簿由 close() 兜底释放。 */
public void write(File output) throws IOException {
try (FileOutputStream fos = new FileOutputStream(output)) {
workbook.write(fos);
}
}
/** 历史骨架的固定收尾:close() 吞异常 + dispose() 释放 SXSSF 临时文件。 */
@Override
public void close() {
try {
workbook.close();
} catch (Exception ignored) {
}
workbook.dispose();
}
// ------------------------------------------------------------------
// 静态零件:空值安全写入与列宽
// ------------------------------------------------------------------
/** null 写成空字符串,其余原样写入(与各模块 safe()/valueOf() 语义一致)。 */
public static void setText(Row row, int column, String value) {
row.createCell(column).setCellValue(value == null ? "" : value);
}
/** 数值单元格:null 写成空字符串,否则写 doubleValue(与 Withdraw writeMoney/CollectData intOrZero 语义一致)。 */
public static void setNumber(Row row, int column, Number value) {
if (value == null) {
row.createCell(column).setCellValue("");
} else {
row.createCell(column).setCellValue(value.doubleValue());
}
}
/** 列宽换算:字符数 * 256。 */
public static int width(int charCount) {
return charCount * 256;
}
/** 按字符数依次设置第 0..n-1 列宽。 */
public static void applyColumnWidths(Sheet sheet, int[] columnWidthChars) {
for (int i = 0; i < columnWidthChars.length; i++) {
sheet.setColumnWidth(i, columnWidthChars[i] * 256);
}
}
}