EasyExcel使用导出模版后设置 CellStyle失效问题解决

EasyExcel使用导出模版后在CellWriteHandler的afterCellDispose方法设置 CellStyle失效问题解决方法

问题描述:excel 模版塞入数据后,需要设置单元格的个性化设置时失效,本文以设置数据格式为例(设置列的数据展示时需要加上千分位分隔符)

1. 无效的解决方案:

在afterCellDispose中通过重新设置 cell 的 CellStyle 来设置 DataFormat,此时可以打印 cell的值和 DataFormat 发现与我们设置的一样,但是导出时就是没有生效。

注意:由于我使用了 easyexcel 中的横向填充数据,所以我将数据全部设置为 String 对应 Excel 中的 String,在下面代码中我删除了部分定制化的逻辑,仅仅只展示如何为本质是 Number的String数据加上DataFormat。

java 复制代码
@Slf4j
public class SignAmtCustTypeMergeStrategy implements CellWriteHandler {


    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {


        if (cellType == CellType.STRING) {
            String stringCellValue = cell.getStringCellValue();
            if (StringUtils.isNotEmpty(stringCellValue)) {
                try {
                    Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
                    CellStyle newStyle = workbook.createCellStyle();
                    CellStyle originalStyle = cell.getCellStyle();
                    if (originalStyle != null) {
                        newStyle.cloneStyleFrom(originalStyle);
                    }
                    newStyle.setDataFormat(IntegerEnum.FOUR.getValue().shortValue());
                    cell.setCellStyle(newStyle);
                    BigDecimal bigDecimal = new BigDecimal(stringCellValue);
                    cell.setCellValue(bigDecimal.doubleValue());
                    
                } catch (Exception e){
                }
            }
        }

    }
}

2. 解决方案:

在执行writeCellData.setWriteCellStyle(writeCellStyle);前后可以对比下面的区别writeCellStyle

  • 具体的区别见下图:index=4代表的是BuiltinFormats的 "#,##0.00",格式。
java 复制代码
@Slf4j
public class SignAmtCustTypeMergeStrategy implements CellWriteHandler {


    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder,
                                 List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
			if (cellType == CellType.STRING) {
			   String stringCellValue = cell.getStringCellValue();
			     if (StringUtils.isNotEmpty(stringCellValue)) {
			         try {
			             BigDecimal bigDecimal = new BigDecimal(stringCellValue);
			             cell.setCellValue(bigDecimal.doubleValue());
			             if (!cellDataList.isEmpty()) {
			                 WriteCellData<?> writeCellData = cellDataList.get(0);
			                 WriteCellStyle writeCellStyle = new WriteCellStyle();
			                 DataFormatData dataFormatData = new DataFormatData();
			                 dataFormatData.setIndex(IntegerEnum.FOUR.getValue().shortValue());
			                 writeCellStyle.setDataFormatData(dataFormatData);
			                 writeCellData.setWriteCellStyle(writeCellStyle);
			             }
			         } catch (Exception e){
			         }
			     }
			 }
      }
}

注意:如果你在执行前你的 cell 数据就是 Number 则不需要执行BigDecimal bigDecimal = new BigDecimal(stringCellValue); cell.setCellValue(bigDecimal.doubleValue());这两行逻辑,如果你想让 String 转 Number并设置DataFormat 则需要设置这段,具体原因还不太清楚,但是不设置DataFormat设置会失败。(有懂得友友可以评论下~~~)

相关推荐
Full Stack Developme5 分钟前
Java后台生成多个Excel并用Zip打包下载
java·开发语言·excel
Brookty8 分钟前
【Java学习】锁、线程死锁、线程安全2
java·开发语言·学习·java-ee
百锦再32 分钟前
.NET 的 WebApi 项目必要可配置项都有哪些?
java·开发语言·c#·.net·core·net
耳东哇43 分钟前
spring ai-openai-vl模型应用qwen-vl\gpt-文字识别-java
java·人工智能·spring
花开富贵ii2 小时前
代码随想录算法训练营四十三天|图论part01
java·数据结构·算法·深度优先·图论
布朗克1683 小时前
Java 10 新特性及具体应用
java·开发语言·新特性·java10
ZZHow10246 小时前
JavaWeb开发_Day05
java·笔记·web
CHEN5_026 小时前
【Java虚拟机】垃圾回收机制
java·开发语言·jvm
Warren986 小时前
Lua 脚本在 Redis 中的应用
java·前端·网络·vue.js·redis·junit·lua
艾伦~耶格尔10 小时前
【数据结构进阶】
java·开发语言·数据结构·学习·面试