EsayExcel让不同标题有不同的颜色

今天我在github社区的时候遇见了这个issues,我有看了下百度发现很多人需要这个问题的解决方案,接下来我就写一份这个问题的解决方案。

您的需求:

​ 您好我了解到您的需求,您的需求是为每一个标题设置不同的颜色。

我的解决方案

​ 首先让esayexcel去创建一个模版文件,然后在根据模版文件去插入数据并且设置不需要标题头。

code

package com.example.easyexcelisusse.excel.Issues3491;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.WriteSheet;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Bryan yang(Dazui) 2023-10-9
 * 解决方案思路,首先生成一个模版文件,然后在根据模版文件进行插入
 */
public class SetCellColorExample {
    public static void main(String[] args) throws IOException {
        String excelFilePath = "colored_cells.xlsx";

        //创建模版文件
        //create template file
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Sheet1");

        //创建模版文件标题头样式
        //create a template file header style
        CellStyle redStyle = workbook.createCellStyle();
        redStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
        redStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        CellStyle greenStyle = workbook.createCellStyle();
        greenStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
        greenStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        CellStyle yellowStyle = workbook.createCellStyle();
        greenStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
        greenStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

        //创建标题头
        //create title style
        Row row = sheet.createRow(0);

        Cell cell1 = row.createCell(0);
        cell1.setCellValue("Cell1");
        cell1.setCellStyle(redStyle);

        Cell cell2 = row.createCell(1);
        cell2.setCellValue("Cell2");
        cell2.setCellStyle(greenStyle);

        Cell cell3 = row.createCell(2);
        cell3.setCellValue("Cell3");
        cell3.setCellStyle(yellowStyle);

        // 保存模版文件
        // save template file
        try (FileOutputStream fos = new FileOutputStream(excelFilePath)) {
            workbook.write(fos);
        }

        System.out.println("Excel save to" + excelFilePath);

        //use template file to insert data
        List<TeamInfo> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            TeamInfo teamInfo = new TeamInfo();
            teamInfo.setC1(i+"");
            teamInfo.setC2(i+"");
            teamInfo.setC3(i+"");
            list.add(teamInfo);
        }


        WriteSheet writeSheet = EasyExcel.writerSheet("Sheet1").head((List<List<String>>) null).build();
        EasyExcel.write("example1.xlsx", TeamInfo.class)
                .withTemplate(excelFilePath)
                .sheet("Sheet1").needHead(false).doWrite(list);

        System.out.println("Excel save to" + excelFilePath);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

哇塞大嘴好帅(DaZuiZui)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值