Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用模版导出,拦截器afterCellDispose指定cell,最终却对全局cell生效 #4050

Open
ryan-sgy opened this issue Nov 17, 2024 · 0 comments
Assignees
Labels
bug Something isn't working

Comments

@ryan-sgy
Copy link

ryan-sgy commented Nov 17, 2024

建议先去看文档

快速开始常见问题

触发场景描述

使用excel模版导出,需要使用拦截器afterCellDispose,根据业务逻辑判定,对指定的cell修改。最终需要修改的样式对全局cell生效。

触发Bug的代码

   public class MyCellWriterHandler implements CellWriteHandler {
    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        Cell cell = context.getCell();
        CellStyle cellStyle = cell.getCellStyle();
        if (cellStyle == null) {
            cellStyle = cell.getSheet().getWorkbook().createCellStyle();  // 创建新的样式
        }
        String sheetName = cell.getRow().getSheet().getSheetName();
       // 这里的代码已经简化了,就是对指定sheet页的指定单元格上背景色
        if (sheetName.equals("基础信息") && cell.getColumnIndex() == 0) {
            cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        }
        cell.setCellStyle(cellStyle);
    }
}

这是我的写入代码:

    public void write(ExcelWriter writer, W data) {
        init();
        for (Map.Entry<String, ExcelSheetAdapter<?>> entry : excelSheetMetaDataMap.entrySet()) {
            String sheetName = entry.getKey();
            WriteSheet writeSheet = EasyExcel.writerSheet(sheetName)
                    .needHead(true)
                    .registerWriteHandler(new MyCellWriterHandler())
                .build();
            writer.write(getData(data, classExcelSheetAdapterMap.getKey(entry.getValue())), writeSheet);
        }
        writer.finish();
    }

提示的异常或者没有达到的效果

所有的单元格都被渲染了灰色背景色

EXPORT_OFFERING_INSTANCE20241117021055_2471.xlsx

@ryan-sgy ryan-sgy added the bug Something isn't working label Nov 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants