This commit is contained in:
2026-08-25 16:15:19 +08:00
parent b8c210fb6b
commit 4e1a162660
27 changed files with 236 additions and 82 deletions
@@ -95,6 +95,20 @@ public class ImportFailureExcelUtil {
* @param excelClass 原导入 Excel 类型
*/
public static void export(HttpServletResponse response, String fileName, String sheetName, List<?> data, Class<?> excelClass) {
exportFailureReasonOnly(response, fileName, sheetName, data, excelClass);
}
/**
* 导出仅标红失败原因列的导入失败明细
*
* @param response 响应
* @param fileName 文件名
* @param sheetName 工作表名
* @param data 失败数据
* @param excelClass 原导入 Excel 类型
*/
public static void exportFailureReasonOnly(HttpServletResponse response, String fileName, String sheetName,
List<?> data, Class<?> excelClass) {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding(StandardCharsets.UTF_8.name());
String encodeFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8);
@@ -104,7 +118,7 @@ public class ImportFailureExcelUtil {
List<List<Object>> rows = buildRows(data, excelFields);
try {
FastExcel.write(response.getOutputStream())
.registerWriteHandler(new ImportFailureCellStyleHandler(excelFields, rows))
.registerWriteHandler(new ImportFailureCellStyleHandler(excelFields.size()))
.head(head)
.sheet(sheetName)
.doWrite(rows);
@@ -182,44 +196,15 @@ public class ImportFailureExcelUtil {
throw new NoSuchFieldException(String.join(",", fieldNames));
}
private static String columnName(Field field) {
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
String[] value = excelProperty.value();
return value.length == 0 ? field.getName() : value[0];
}
private static String normalize(String value) {
return value == null ? "" : value.replaceAll("[\\s*_:,。;;()()\\[\\]【】<>《》-]", "").toLowerCase();
}
private static List<String> columnKeywords(Field field) {
String columnName = columnName(field);
List<String> keywords = new ArrayList<>();
keywords.add(columnName);
keywords.add(field.getName());
keywords.addAll(Arrays.asList(columnName.replace("*", "").split("[//、()()\\s]+")));
return keywords.stream()
.map(ImportFailureExcelUtil::normalize)
.filter(keyword -> keyword.length() >= 2)
.distinct()
.toList();
}
private static class ImportFailureCellStyleHandler implements CellWriteHandler {
private final List<List<String>> columnKeywords;
private final List<List<Object>> rows;
private final int failureReasonColumnIndex;
private final Map<Short, CellStyle> redStyleCache = new HashMap<>();
private final Map<Short, CellStyle> noWrapStyleCache = new HashMap<>();
private final Map<Integer, Integer> columnWidthCache = new HashMap<>();
private ImportFailureCellStyleHandler(List<Field> excelFields, List<List<Object>> rows) {
this.columnKeywords = excelFields.stream()
.map(ImportFailureExcelUtil::columnKeywords)
.toList();
this.rows = rows;
this.failureReasonColumnIndex = excelFields.size();
private ImportFailureCellStyleHandler(int failureReasonColumnIndex) {
this.failureReasonColumnIndex = failureReasonColumnIndex;
}
@Override
@@ -237,25 +222,11 @@ public class ImportFailureExcelUtil {
return;
}
adjustColumnWidth(cell);
if (relativeRowIndex == null || relativeRowIndex < 0 || relativeRowIndex >= rows.size()) {
return;
}
if (shouldMarkRed(relativeRowIndex, cell.getColumnIndex())) {
if (cell.getColumnIndex() == failureReasonColumnIndex) {
markRed(cell);
}
}
private boolean shouldMarkRed(int rowIndex, int columnIndex) {
if (columnIndex == failureReasonColumnIndex) {
return true;
}
if (columnIndex < 0 || columnIndex >= columnKeywords.size()) {
return false;
}
String failureReason = normalize(String.valueOf(rows.get(rowIndex).get(failureReasonColumnIndex)));
return columnKeywords.get(columnIndex).stream().anyMatch(failureReason::contains);
}
private void markRed(Cell cell) {
CellStyle currentStyle = cell.getCellStyle();
CellStyle redStyle = redStyleCache.computeIfAbsent(currentStyle.getIndex(), styleIndex -> {