EasyExcel导出数据根据导出值大小,设置字体颜色

需求描述导出的Excel文档中,存在一列有效期(为Int类型),需要根据数值的大小来确定导出Excel的文本的颜色----记录过程

解决方案是在生成Excel单元格后,增加拦截器,对单元格的样式进行操作,代码如下:

EasyExcel.write(response.getOutputStream(), LifeGetStockHistoryResDTO.class)
    .inMemory(true)
    .registerWriteHandler(new CustomCellWriteHandler())//自定义样式
    .sheet().doWrite(export);

拦截器代码:

public class CustomCellWriteHandler implements CellWriteHandler {

    /**
     * 生成的Excel表格的第9列
     */
    private static final Integer COLUMN_INDEX = 9;
    /**
     * 有效期的区间数字_60
     */
    private static final Integer NUMBER_60 = 60;
    /**
     * 有效期的区间数字_30
     */
    private static final Integer NUMBER_30 = 30;
    /**
     * 有效期的区间数字_0
     */
    private static final Integer NUMBER_0 = 0;

    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        if (BooleanUtils.isNotTrue(context.getHead())) {
            Cell cell = context.getCell();
            Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();
            // 这里千万记住 想办法能复用的地方把他缓存起来 一个表格最多创建6W个样式
            // 不同单元格尽量传同一个 cellStyle
            CellStyle cellStyle = workbook.createCellStyle();
            int columnIndex = cell.getColumnIndex();
            if (cell.getColumnIndex() == COLUMN_INDEX ) {
                String stringCellValue = cell.getStringCellValue();
                int count = Integer.parseInt(stringCellValue);
                Font writeFont = workbook.createFont();
                if (count > NUMBER_60){
                    writeFont.setColor(IndexedColors.GREEN.getIndex());
                }else if (count >= NUMBER_30){
                    writeFont.setColor(IndexedColors.YELLOW.getIndex());
                }else if (count >= NUMBER_0){
                    writeFont.setColor(IndexedColors.RED.getIndex());
                }else {
                    writeFont.setColor(IndexedColors.GREY_25_PERCENT.getIndex());
                }
                cellStyle.setBorderLeft(BorderStyle.THIN);
                cellStyle.setBorderRight(BorderStyle.THIN);
                cellStyle.setBorderBottom(BorderStyle.THIN);
                cell.setCellValue(count+"天");
                //设置单元格背景色
//                  cellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
                    cellStyle.setFont(writeFont);
                    // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND
                    cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                    cell.setCellStyle(cellStyle);
                    // 由于这里没有指定dataformat 最后展示的数据 格式可能会不太正确
                    // 这里要把 WriteCellData的样式清空, 不然后面还有一个拦截器 FillStyleCellWriteHandler 默认会将 WriteCellStyle 设置到
                    // cell里面去 会导致自己设置的不一样
                    context.getFirstCellData().setWriteCellStyle(null);

            }
        }
    }
}

PS:其他实现可以参考官网链接:

EasyExcel官方文档 - 基于Java的Excel处理工具 | Easy Excel

  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值