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

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用EasyExcel实现多级表头导出EasyExcel是一个基于Java的简单易用的Excel操作工具,支持大数据量的导入导出操作。 下面是一个示例代码,演示了如何实现多级表头导出: ```java // 创建一个excel写对象 ExcelWriter writer = EasyExcel.write("output.xlsx").build(); // 定义表头数据 List<List<String>> head = new ArrayList<>(); // 第一行表头 List<String> headRow1 = new ArrayList<>(); headRow1.add("一级表头"); headRow1.add("一级表头"); headRow1.add("一级表头"); head.add(headRow1); // 第二行表头 List<String> headRow2 = new ArrayList<>(); headRow2.add("二级表头"); headRow2.add("二级表头"); headRow2.add("二级表头"); head.add(headRow2); // 第三行表头 List<String> headRow3 = new ArrayList<>(); headRow3.add("三级表头"); headRow3.add("三级表头"); headRow3.add("三级表头"); head.add(headRow3); // 写入表头数据 Sheet sheet = new Sheet(1, 0); sheet.setHead(head); writer.write1(null, sheet); // 写入内容数据(省略) // 关闭excel写对象 writer.finish(); ``` 在上面的示例代码中,我们创建了一个ExcelWriter对象,并指定了输出文件名为"output.xlsx"。然后,我们定义了一个包含多级表头表头数据,每一级的表头都是一个List<String>。我们将这些表头数据添加到head列表中,然后使用Sheet对象将head列表设置为excel的表头。最后,调用writer的write1方法写入表头数据。 你可以根据需要修改示例代码中的表头数据,然后使用EasyExcel提供的其他方法写入内容数据。完成后,调用writer的finish方法关闭excel写对象即可。 希望对你有帮助!如果还有其他问题,请继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值