POI设置Excel单元格背景色

1、问题描述?

通过POI的XSSFWorkbook+Java导出数据到Excel表格的时候,给特殊的数据及列加上背景色能突出数据的重要性。

2、给单元格加入背景色?

主要就是CellStyle 的应用

复制代码
//创建一个模板表格,直接创建一个就可以了。
File file=new File("C:\\Users\\Administrator\\Desktop\\test.xlsx");
InputStream in=new FileInputStream(file);
XSSFWorkbook  workbook=new XSSFWorkbook(in);

//创建sheet页
XSSFSheet sheetNEW = workbook.createSheet("sheet页");
//通过sheet创建row
XSSFRow rowNew = sheetNEW.createRow(0);
//通过row创建单元格cell
XSSFCell cell0 = rowNew .createCell(0);


// 创建单元格样式
CellStyle cellStylet0 = workbook.createCellStyle();

// 设置背景色为黄色
cellStylet0 .setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());
cellStylet0 .setFillPattern(FillPatternType.SOLID_FOREGROUND);
//设置单元格格式
cell0 .setCellStyle(cellStylet0 );

3、设置单元格的前景色

复制代码
// Orange "foreground", foreground being the fill foreground not the font color.
style = workbook.createCellStyle();
style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell = row.createCell(2);
cell.setCellValue("X");
cell.setCellStyle(style);
相关推荐
Full Stack Developme1 天前
Java后台生成多个Excel并用Zip打包下载
java·开发语言·excel
芦骁骏2 天前
自动处理考勤表——如何使用Power Query,步步为营,一点点探索自定义函数
数据分析·excel·powerbi
用户8356290780513 天前
使用 C# 将 DataTable 写入 Excel(基于 Spire.XLS for .NET)
excel
迪尔~3 天前
Apache POI中通过WorkBook写入图片后出现导出PDF文件时在不同页重复写入该图片问题,如何在通过sheet获取绘图对象清除该图片
java·pdf·excel
瓶子xf5 天前
使用Excel制作甘特图
excel·甘特图
战族狼魂5 天前
Excel 连接阿里云 RDS MySQL
mysql·阿里云·云计算·excel
cypking5 天前
vue excel转json功能 xlsx
vue.js·json·excel
专注VB编程开发20年5 天前
C#教程之NPOI读写excel文件XLS,XLSX格式
数据库·microsoft·c#·excel·xlsx·xls
YC运维5 天前
WEB虚拟主机3种部署方式全解析
excel
Dxy12393102168 天前
Python如何合并两个Excel文件
爬虫·python·excel