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

原文地址:https://blog.csdn.net/eugene03/article/details/134779535

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_41068.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注