• 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 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>) null).build();
            EasyExcel.write("example1.xlsx", TeamInfo.class)
                    .withTemplate(excelFilePath)
                    .sheet("Sheet1").needHead(false).doWrite(list);
    
            System.out.println("Excel save to" + excelFilePath);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
  • 相关阅读:
    边坡监测系统:全天监测、智能预警
    Go 语言结构体验证详解:validate 标签与自定义规则
    OceanBase荣获OSCAR两项大奖,开源已成主流开发模式
    【电源专题】案例:电源芯片对输入索取大电流的异常开发板测试能出现,但整机就无法复现?
    基于粒子群优化的神经网络PID控制(Matlab)代码实现
    1205、mysql视图、mysql存储过程
    化合物的四种基本反应与氧化还原反应;化合物极性与非极性;亲核反应与亲电反应
    C# SortedList类:有序列表
    前端面试题JavaScript篇——2022-09-16
    漏洞复现----48、Airflow dag中的命令注入(CVE-2020-11978)
  • 原文地址:https://blog.csdn.net/qq_40102411/article/details/133706617