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

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

java 复制代码
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);

相关推荐
CodeMartain10 分钟前
shardingsphere-spring 实现数据分片(一)
java·后端·spring
hhb_61819 分钟前
C Shell脚本编程与系统管理技术实践指南
java·c语言·开发语言
小雅痞20 分钟前
[Java][Leetcode hard] 68. 文本左右对齐
java·开发语言·leetcode
一直跑21 分钟前
同一台服务器上(同局域网)的其他账号访问自己的数据(没有sudo权限和无 ACL和无共同组)
java·linux·服务器
梦想的颜色24 分钟前
js document 节点增删改查、样式设计全解析
java·前端·javascript
老友@30 分钟前
Jenkins 中 Node 版本异常排查:Alpine + musl 导致的兼容问题(lts-alpine-jdk17)
java·servlet·jenkins
笛卡尔的心跳30 分钟前
Spring MVC 注解
java·spring·mvc
Kiyra36 分钟前
为什么远程调用别包进 Spring 事务里
java·后端·spring
人道领域44 分钟前
【LeetCode刷题日记】225.用队列实现栈--三招实现栈操作(多种思维)
java·开发语言·算法·leetcode·面试
Mr_pyx1 小时前
【告别for循环】Java Stream 流式编程精通:从入门到源码级的性能优化
java·开发语言·性能优化