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

相关推荐
IT毕设梦工厂1 小时前
计算机毕业设计选题推荐-在线拍卖系统-Java/Python项目实战
java·spring boot·python·django·毕业设计·源码·课程设计
Ylucius1 小时前
动态语言? 静态语言? ------区别何在?java,js,c,c++,python分给是静态or动态语言?
java·c语言·javascript·c++·python·学习
七夜zippoe2 小时前
分布式系统实战经验
java·分布式
是梦终空2 小时前
JAVA毕业设计176—基于Java+Springboot+vue3的交通旅游订票管理系统(源代码+数据库)
java·spring boot·vue·毕业设计·课程设计·源代码·交通订票
落落落sss2 小时前
sharding-jdbc分库分表
android·java·开发语言·数据库·servlet·oracle
码爸2 小时前
flink doris批量sink
java·前端·flink
工业互联网专业3 小时前
毕业设计选题:基于springboot+vue+uniapp的驾校报名小程序
vue.js·spring boot·小程序·uni-app·毕业设计·源码·课程设计
Monodye3 小时前
【Java】网络编程:TCP_IP协议详解(IP协议数据报文及如何解决IPv4不够的状况)
java·网络·数据结构·算法·系统架构
一丝晨光3 小时前
逻辑运算符
java·c++·python·kotlin·c#·c·逻辑运算符
无名指的等待7123 小时前
SpringBoot中使用ElasticSearch
java·spring boot·后端