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

相关推荐
米码收割机7 小时前
【项目】spring boot+vue3 宠物领养系统(源码+文档)【独一无二】
java·spring boot·宠物
刹那芳华19928 小时前
STMF+ESP-S+MQTT协议连接华为云端(附踩坑记录)
java·struts·华为
就改了13 小时前
Java8 日期处理(详细版)
java·python·算法
麻瓜生活睁不开眼14 小时前
Android16定制SearchLauncher新增投屏Cast桌面快捷图标完整实现
java·launcher·aosp16
IT小盘15 小时前
12-Prompt不等于一句话-System-User-Context三层结构
java·windows·prompt
都叫我大帅哥16 小时前
Spring Data JPA 查询之道:方法命名与示例查询完全指南
java·后端·spring
momo16 小时前
Java+WebAI黑马知识点
java·spring boot·mybatis
重庆小透明18 小时前
带你从不同视角了解三大MQ
java·学习·spring·kafka·rabbitmq·rocketmq
冰心孤城18 小时前
C++ 与 C#混合编程 示例 (基于VS)
java·c++·c#
风中芦苇啊20 小时前
Java EasyExcel 导入通用工具类:自定义注解映射字段 + 反射机制
java·开发语言