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);
相关推荐
yongui478343 小时前
使用C#实现Excel实时读取并导入SQL数据库
数据库·c#·excel
hanjq_code4 小时前
java使用阿里的easyExcel解决把excel每行的数据转成excel表格格式数据并打包成ZIP下载
java·开发语言·excel
牛猫Data5 小时前
Power BI为什么不能完全取代Excel?
microsoft·数据分析·excel·database·数据可视化·powerbi
小当家.1055 小时前
《Java操作Excel实战教程:Apache POI从入门到精通》
java·apache·excel
vortex51 天前
【小技巧】用 VLOOKUP 实现表格合并
excel
宫瑾1 天前
Excel常用操作记录
excel
老蒋每日coding1 天前
Java解析Excel并对特定内容做解析成功与否的颜色标记
java·开发语言·excel
博风1 天前
bartender打印软件连接excel文档进行标签打印
excel
焦思懿--19期--工职大1 天前
[特殊字符]Linux文件查看命令全面对比解析:从基础cat到高级文本处理工具
excel
ekkcole1 天前
java实现对excel文件合并单元格(只针对文件)
java·开发语言·excel