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);
相关推荐
Hello.Reader2 小时前
Flink 系统内置函数(Built-in Functions)分类、典型用法与选型建议
大数据·flink·excel
彭于晏Yan8 小时前
excel导入导出
spring boot·excel
lbb 小魔仙10 小时前
Python 读取 Excel 文件:openpyxl 与 pandas 实战对比
python·excel·pandas
徐赛俊11 小时前
# Excel 图表自动变色教程(基于控制单元格)
excel
2501_9307077812 小时前
如何使用C#代码将 Excel 中的图表转换为图片
excel
娃乐呵1 天前
免费的大批量Excel文档大模型处理数据工具
语言模型·大模型·excel·数据处理
Eiceblue1 天前
使用 Python 写入多类型数据至 Excel 文件
开发语言·python·excel
蹦蹦跳跳垂耳兔1 天前
Excel创建组妙用隐藏
excel
开开心心_Every1 天前
无广告干扰:简单好用文字LOGO设计工具
xml·java·网络·数据库·华为od·华为云·excel
CodeCraft Studio2 天前
Excel处理控件Aspose.Cells教程:使用C#在Excel中创建气泡图
信息可视化·c#·excel·aspose·excel api库·excel气泡图·excel组件库