当前位置: 首页 > news >正文

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 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 dataList<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);}
}
http://www.lryc.cn/news/188396.html

相关文章:

  • Ant Design of React 创建项目及运行环境
  • NoSQL之Redis 主从复制配置详解及哨兵模式
  • 界面组件DevExpress WinForms v23.2新功能预览 - 增强MVVM相关功能
  • vue3前端开发-开发环境安装篇
  • MVCC和BufferPool缓存机制
  • 论文阅读笔记(Clover: 计算与存储被动分离的分布式键值存储系统)
  • 大模型推理框架概述
  • 抖音商品详情数据接口,抖音商品详情API接口
  • 睿趣科技:抖音开网店怎么开通
  • 体育场馆能源消耗监测管理平台,为场馆提供能源服务
  • 前端本地存储方案-localForage-vue3中使用
  • vivo年度新“机皇”X100系列要来了!
  • 滴滴发布十一大数据:延边出行需求上涨280% 西部省份成旅游热点
  • Allegro如何查看器件的管脚号?
  • 苹果电脑用什么清理软件比较好?
  • 被装超市管理系统-智被装DW-S305系统组成项目背景
  • 使用“Apple 诊断”测试你的 Mac(查看电池是否到达更换标准)
  • ElementUI增删改的实现及表单验证
  • 基于Google‘s FCM实现消息推送
  • 若依微服务前后端部署启动流程(只记录)
  • docker创建nginx容器
  • nio 文件传输
  • 2023-10-09 python-安装psd_tools-记录
  • 【Python目标识别】目标检测的原理及常见模型的介绍
  • SpringCloud小项目——订单积分商城 使用Nacos、Open Feign、Gateway、Sentinel技术栈
  • 澳大利亚教育部宣布ChatGPT将被允许在澳学校使用!
  • Linux基础指令笔记大全
  • vue学习-14vue的路由缓存组件以及activated和deactivated生命周期钩子
  • 【opencv】windows10下opencv4.8.0-cuda版本源码编译教程
  • 31 数据分析(中)numpy介绍