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);
相关推荐
Dfreedom.8 小时前
Excel文件数据的读取和处理方法——C++
c++·数据分析·excel·数据预处理
开开心心就好10 小时前
Word批量转PDF工具
开发语言·人工智能·pdf·c#·vim·excel·语音识别
CodeCraft Studio1 天前
Excel处理控件Aspose.Cells教程:使用 C# 在 Excel 中创建组合图表
c#·excel·aspose·图表
小镇学者1 天前
【PHP】导入excel 报错Trying to access array offset on value of type int
android·php·excel
CodeCraft Studio1 天前
Excel处理控件Aspose.Cells教程:使用 C# 从 Excel 进行邮件合并
开发语言·c#·excel
CodeCraft Studio1 天前
Excel处理控件Aspose.Cells教程:使用 C# 在 Excel 中应用数据验证
c#·excel·aspose·文档开发·文档处理
只有干货1 天前
java若依 excel 导出高度自适应
java·excel
饼干哥哥2 天前
3个实操案例,学会用DeepSeek做VBA开发实现Excel自动化
excel·deepseek
CodeCraft Studio2 天前
国产化Excel处理组件Spire.XLS教程:用 Java 获取所有 Excel 工作表名称(图文详解)
java·excel·数据处理·spire