东莞知名网站,网站建设费用文档,最新经济新闻头条新闻,移动端是什么今天我在github社区的时候遇见了这个issues#xff0c;我有看了下百度发现很多人需要这个问题的解决方案#xff0c;接下来我就写一份这个问题的解决方案。
您的需求#xff1a;
您好我了解到您的需求#xff0c;您的需求是为每一个标题设置不同的颜色。
我的解决方案…今天我在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 fileWorkbook workbook new XSSFWorkbook();Sheet sheet workbook.createSheet(Sheet1);//创建模版文件标题头样式//create a template file header styleCellStyle 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 styleRow 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 filetry (FileOutputStream fos new FileOutputStream(excelFilePath)) {workbook.write(fos);}System.out.println(Excel save to excelFilePath);//use template file to insert dataListTeamInfo 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((ListListString) null).build();EasyExcel.write(example1.xlsx, TeamInfo.class).withTemplate(excelFilePath).sheet(Sheet1).needHead(false).doWrite(list);System.out.println(Excel save to excelFilePath);}
}