1、调整导入失败,导出excel功能

2、调整规范
This commit is contained in:
2026-07-27 14:53:28 +08:00
parent 4b6908d3bf
commit 74416dabaf
2 changed files with 92 additions and 11 deletions

View File

@@ -28,11 +28,17 @@ package org.springblade.common.excel;
import cn.idev.excel.FastExcel;
import cn.idev.excel.annotation.ExcelIgnore;
import cn.idev.excel.annotation.ExcelProperty;
import cn.idev.excel.write.metadata.style.WriteCellStyle;
import cn.idev.excel.write.metadata.style.WriteFont;
import cn.idev.excel.write.style.HorizontalCellStyleStrategy;
import cn.idev.excel.metadata.data.WriteCellData;
import cn.idev.excel.write.handler.CellWriteHandler;
import cn.idev.excel.write.handler.context.CellWriteHandlerContext;
import cn.idev.excel.write.metadata.holder.WriteSheetHolder;
import cn.idev.excel.write.metadata.holder.WriteTableHolder;
import jakarta.servlet.http.HttpServletResponse;
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.IndexedColors;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.IOException;
import java.lang.reflect.Field;
@@ -40,7 +46,9 @@ import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
@@ -76,7 +84,7 @@ public class ImportFailureExcelUtil {
List<List<Object>> rows = buildRows(data, excelFields);
try {
FastExcel.write(response.getOutputStream())
.registerWriteHandler(failureRowStyle())
.registerWriteHandler(new ImportFailureCellStyleHandler(excelFields, rows))
.head(head)
.sheet(sheetName)
.doWrite(rows);
@@ -154,13 +162,85 @@ public class ImportFailureExcelUtil {
throw new NoSuchFieldException(String.join(",", fieldNames));
}
private static HorizontalCellStyleStrategy failureRowStyle() {
WriteFont contentFont = new WriteFont();
contentFont.setColor(IndexedColors.RED.getIndex());
private static String columnName(Field field) {
ExcelProperty excelProperty = field.getAnnotation(ExcelProperty.class);
String[] value = excelProperty.value();
return value.length == 0 ? field.getName() : value[0];
}
WriteCellStyle contentStyle = new WriteCellStyle();
contentStyle.setWriteFont(contentFont);
return new HorizontalCellStyleStrategy(null, contentStyle);
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 ImportFailureCellStyleHandler(List<Field> excelFields, List<List<Object>> rows) {
this.columnKeywords = excelFields.stream()
.map(ImportFailureExcelUtil::columnKeywords)
.toList();
this.rows = rows;
this.failureReasonColumnIndex = excelFields.size();
}
@Override
public void afterCellDispose(
WriteSheetHolder writeSheetHolder,
WriteTableHolder writeTableHolder,
List<WriteCellData<?>> cellDataList,
Cell cell,
cn.idev.excel.metadata.Head head,
Integer relativeRowIndex,
Boolean isHead) {
if (Boolean.TRUE.equals(isHead) || relativeRowIndex == null || relativeRowIndex < 0 || relativeRowIndex >= rows.size()) {
return;
}
if (shouldMarkRed(relativeRowIndex, cell.getColumnIndex())) {
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 -> {
Workbook workbook = cell.getSheet().getWorkbook();
CellStyle newStyle = workbook.createCellStyle();
newStyle.cloneStyleFrom(currentStyle);
Font font = workbook.createFont();
font.setColor(IndexedColors.RED.getIndex());
newStyle.setFont(font);
return newStyle;
});
cell.setCellStyle(redStyle);
}
}
}