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
@@ -2,15 +2,13 @@ package com.nanri.aiimage.modules.publish.service;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.util.ExcelStreamReader;
import com.nanri.aiimage.common.util.SxssfWorkbookWriter;
import com.nanri.aiimage.modules.publish.model.dto.PublishRowDto;
import org.apache.poi.ss.usermodel.Cell;
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.ss.usermodel.Workbook;
import org.apache.poi.ss.util.WorkbookUtil;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;
import java.io.File;
@@ -96,27 +94,19 @@ public class PublishWorkbookService {
if (parent != null) {
parent.mkdirs();
}
SXSSFWorkbook workbook = new SXSSFWorkbook(200);
workbook.setCompressTempFiles(true);
try (FileOutputStream output = new FileOutputStream(outputFile)) {
CellStyle headerStyle = createHeaderStyle(workbook);
try (SxssfWorkbookWriter writer = SxssfWorkbookWriter.open()) {
CellStyle headerStyle = writer.headerStyle();
String country = countrySheetName(publishCountry);
// 工作表名必须是 Excel 合法名(≤31 字符、不含 []:*?/\ ),而 country 来自接口参数,
// 用户填了长文本/特殊字符时直接 createSheet 会抛异常导致"生成上架结果 Excel 失败"。
// uniqueSheetName 是项目内已有的净化样板(dedupe 同款 WorkbookUtil.createSafeSheetName),
// 此处只净化工作表名;单元格里的国家值仍按原口径写入。
Sheet sheet = workbook.createSheet(uniqueSheetName(country, new LinkedHashSet<>()));
Sheet sheet = writer.workbook().createSheet(uniqueSheetName(country, new LinkedHashSet<>()));
writeSheet(sheet, rows, headerStyle, country);
workbook.write(output);
writer.write(outputFile);
return outputFile;
} catch (Exception ex) {
throw new BusinessException("生成上架结果 Excel 失败: " + safeMessage(ex));
} finally {
workbook.dispose();
try {
workbook.close();
} catch (Exception ignored) {
}
}
}
@@ -228,20 +218,10 @@ public class PublishWorkbookService {
setText(row, 7, value.getSyncCountries());
}
int[] widths = {14, 18, 12, 24, 12, 14, 14, 28};
for (int index = 0; index < widths.length; index++) {
sheet.setColumnWidth(index, widths[index] * 256);
}
SxssfWorkbookWriter.applyColumnWidths(sheet, widths);
sheet.createFreezePane(0, 1);
}
private CellStyle createHeaderStyle(Workbook workbook) {
Font font = workbook.createFont();
font.setBold(true);
CellStyle style = workbook.createCellStyle();
style.setFont(font);
return style;
}
private void setText(Row row, int columnIndex, String value) {
row.createCell(columnIndex).setCellValue(value == null ? "" : value);
}