[alibaba/easyexcel]使用doFill()形式写excel时,自定义实现CellWriteHandler接口会生效吗

2024-04-25 710 views
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模板 image 导出结果 image

回答

9

不知道您是否已经解决该问题?我们先暂时关闭这个问题,如果还存在问题请重新Reopen这个issue