easyexcel多级表头导出各级设置样式(继承HorizontalCellStyleStrategy实现)

easyexcel多级表头导出各级设置样式(继承HorizontalCellStyleStrategy实现)

package com.example.wxmessage.entity;

import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;

/**
 * @author chenyuhuan
 * @ClassName CellStyleStrategy.java
 * @Description TODO
 * @createTime 2023年12月01日
 */
public class CellStyleStrategy extends HorizontalCellStyleStrategy {
   


    private final WriteCellStyle headWriteCellStyle;

    public CellStyleStrategy(WriteCellStyle headWriteCellStyle) {
   
        this.headWriteCellStyle = headWriteCellStyle;
    }

    @Override
    protected void setHeadCellStyle(CellWriteHandlerContext context) {
   


        // 根据行索引为不同级别的表头应用不同样式
        if (context.getRowIndex() == 0) {
   
         
            headWriteCellStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
            headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);


        } else if (context.getRowIndex() == 1) {
   
      
            headWriteCellStyle.setFillForegroundColor(IndexedColors.WHITE.getIndex());
            headWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        }

        if (stopProcessing(context)) {
   
            return;
        }
        WriteCellData<?> cellData = context.getFirstCellData();
        WriteCellStyle.merge(this.headWriteCellStyle, cellData.getOrCreateStyle());
    }
}


###########################调用###############################
  EasyExcel.write(response.getOutputStream(), ParamWorkdaysExportExcel.class)
    .registerWriteHandler(new CellStyleStrategy(new WriteCellStyle()))
    .sheet("模板")
    .doWrite(data);

image.png1701659747655.png

相关推荐

  1. 通过easyExcel实现表格导入导出

    2023-12-08 12:30:04       70 阅读
  2. EasyExcel实现导入导出

    2023-12-08 12:30:04       25 阅读
  3. EasyExcel多行表头带动态下拉框导入导出具体实现

    2023-12-08 12:30:04       33 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-08 12:30:04       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-08 12:30:04       97 阅读
  3. 在Django里面运行非项目文件

    2023-12-08 12:30:04       78 阅读
  4. Python语言-面向对象

    2023-12-08 12:30:04       88 阅读

热门阅读

  1. 处理合并目录下的Excel文件数据并指定列去重

    2023-12-08 12:30:04       56 阅读
  2. Next.js 中的中间件

    2023-12-08 12:30:04       47 阅读
  3. [C#]文件的读写-1

    2023-12-08 12:30:04       55 阅读
  4. zookeeper常用接口

    2023-12-08 12:30:04       51 阅读
  5. 二维码扫描并输出信息(小程序,IOS,安卓)

    2023-12-08 12:30:04       43 阅读
  6. 《微信小程序开发从入门到实战》学习四十四

    2023-12-08 12:30:04       58 阅读
  7. 数据结构-数组

    2023-12-08 12:30:04       55 阅读
  8. 搜索引擎高级用法总结: 谷歌、百度、必应

    2023-12-08 12:30:04       64 阅读
  9. stm8l151,c语言混编汇编,实现16位乘除法

    2023-12-08 12:30:04       52 阅读
  10. Flink 项目系列

    2023-12-08 12:30:04       69 阅读
  11. flink sink多个topic

    2023-12-08 12:30:04       49 阅读
  12. PTA 7-237 特殊排序

    2023-12-08 12:30:04       53 阅读