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);

相关推荐
Slow菜鸟41 分钟前
SpringBoot集成Elasticsearch | Elasticsearch 8.x专属Java Client
java·spring boot·elasticsearch
Miraitowa_cheems42 分钟前
LeetCode算法日记 - Day 82: 环形子数组的最大和
java·数据结构·算法·leetcode·决策树·线性回归·深度优先
豐儀麟阁贵1 小时前
4.5数组排序算法
java·开发语言·数据结构·算法·排序算法
Halo_tjn2 小时前
Java Map集合
java·开发语言·计算机
程序猿小蒜3 小时前
基于springboot的车辆管理系统设计与实现
java·数据库·spring boot·后端·spring·oracle
zl9798993 小时前
SpringBoot-Web开发之Web原生组件注入
java·spring boot·spring
小羊学伽瓦3 小时前
【Java数据结构】——常见力扣题综合
java·数据结构·leetcode·1024程序员节
I'm Jie4 小时前
(五)Gradle 依赖传递与冲突处理
java·spring boot·spring·kotlin·gradle·maven
我命由我123454 小时前
Spring Cloud - Spring Cloud 声明式接口调用(Fiegn 声明式接口调用概述、Fiegn 使用)
java·后端·spring·spring cloud·微服务·架构·java-ee
_extraordinary_4 小时前
Java Spring事务,事务的传播机制
java·开发语言·spring