8
异常代码
实现CellWriteHandler接口
public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
final var cellStyle = cellStyle(workbook);
try {
if (cell.getRowIndex() > 1 && cell.getColumnIndex() == 2) {
if (cell.getNumericCellValue() < 18D) {
cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
}
}
} catch (ParseException e) {
e.printStackTrace();
}
cell.setCellStyle(cellStyle);
}
public static void main(String[] args) {
final var date = Map.of("date", "2023-04-22");
final List<Map<String, Object>> lstData = List.of(
Map.of("id", 1, "name", "哈哈", "age", 17),
Map.of("id", 2, "name", "嘻嘻", "age", 18),
Map.of("id", 3, "name", "呵呵", "age", 19)
);
String file = "/Users/BOOMXIKA/Desktop/export.xlsx";
String exportFile = "/Users/BOOMXIKA/Desktop/gogogo.xlsx";
try (ExcelWriter excelWriter = EasyExcel.write(exportFile).withTemplate(file).registerWriteHandler(new SignRecordExcelHandler()).build()) {
WriteSheet writeSheet = EasyExcel.writerSheet().build();
excelWriter.fill(lstData, writeSheet);
excelWriter.fill(date, writeSheet);
}
}
异常提示
打断点看到方法成功执行到cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());语句,但是最终excel中的年龄17单元格并无标红
问题描述excel模板 导出结果