alibaba / easyexcel

快速、简洁、解决大文件内存溢出的java处理Excel工具
https://easyexcel.opensource.alibaba.com
Apache License 2.0
32.09k stars 7.5k forks source link

orginStyle与writeStyle的谁覆盖谁的疑惑 #3942

Closed brged closed 3 weeks ago

brged commented 1 month ago

版本

3.2.1

触发场景描述

afterCellDispose(CellWriteHandlerContext context) 方法中改变样式,setCellStyle中看到writeCellStyle和originCellStyle

触发Bug的代码

WriteCellData<?> firstCellData = context.getFirstCellData();
WriteCellStyle defaultWriteCellStyle = firstCellData.getWriteCellStyle();
// 重置样式
firstCellData.setWriteCellStyle(new WriteCellStyle());
firstCellData.setOriginCellStyle(workbook.createCellStyle());
// 给writeCellStyle设置背景色和字体
firstCellData.getWriteCellStyle().setFillPatternType(FillPatternType.SOLID_FOREGROUND);
firstCellData.getWriteCellStyle().setFillBackgroundColor(IndexedColors.RED.getIndex()); //红

WriteFont writeFont = new WriteFont();
writeFont.setColor(IndexedColors.RED.getIndex()); // 红
firstCellData.getWriteCellStyle().setWriteFont(writeFont);
// 给originCellStyle设置背景色和字体
firstCellData.setOriginCellStyle(workbook.createCellStyle());
firstCellData.getOriginCellStyle().setFillPattern(FillPatternType.SOLID_FOREGROUND);
firstCellData.getOriginCellStyle().setFillForegroundColor(IndexedColors.BLUE.getIndex()); // 蓝

Font font = workbook.createFont();
font.setColor(IndexedColors.BLUE.getIndex());
firstCellData.getOriginCellStyle().setFont(font); // 蓝

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

输出样式是蓝底红字 $GV7RGOMH_F(2TDNNRPR%F9

然后看 WriteWorkbookHolder.createCellStyle(WriteCellStyle writeCellStyle, CellStyle originCellStyle) 方法中 样式生效顺序是 writeCellStyle, originCellStyle 字体生效顺序是 originFont, writeFont 内容格式化是最终样式的格式化

brged commented 3 weeks ago

用4.0.2测试没问题的,优先级writeStyle > orginStyle,context.getCell().getCellStyle()的设置无效