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);
相关推荐
X@AKS6 小时前
解决使用EasyExcel导出带公式的excel,公式不自动计算问题
excel
Wang201220137 小时前
wps excel中把特定几列除以某一列,然后以百分比显示
excel
LilySesy8 小时前
ABAP+在select的时候,可以A=B A=C B=C这样子JOIN吗?
数据库·sql·ai·excel·sap·abap
zhishidi10 小时前
Excel表格自适应大小设置方法
excel
缺点内向17 小时前
C#: 高效移动与删除Excel工作表
开发语言·c#·.net·excel
程序员晚枫1 天前
Python处理Excel的5个“神仙库”,办公效率直接翻倍!
python·excel
_处女座程序员的日常1 天前
如何预览常见格式word、excel、ppt、图片等格式的文档
前端·javascript·word·excel·开源软件
best_scenery1 天前
excel T检测时[检验类型]参数设置的方法
excel
路漫漫其修远.1 天前
解决excel复制页面行高无法复制的问题
excel
办公解码器1 天前
超链接查看太麻烦,Excel怎么快速提取单元格内的超链接地址?
excel