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);
相关推荐
许彰午1 天前
在PowerBuilder里手写Excel导出——OLE控制Excel的完整方案
excel
admin0051 天前
进销存软件与Excel的边界:什么时候该从Excel升级到专业软件
excel·企业管理·财务管理·进销存管理
灵析表格1 天前
json_ObjectToKV 函数深度研究报告
json·excel·wps·灵析表格·excel公式盒子
观远数据2 天前
Excel、自研、还是换BI?产品VP拆解三条数据分析路线的隐性成本
大数据·数据分析·excel
娇气的妮巴2 天前
老Excel数据怎么迁到ERP系统
java·前端·excel
2601_965912832 天前
PDF转Excel工具横向评测:6款免费方案实测数据对比
pdf·excel·notepad++
一行注释2 天前
纯前端通过xlsx.js导出Excel表格:完整示例与实战指南
前端·javascript·excel
QuartusII72 天前
Excel中alt+enter单元格内无法换行
excel
Arya_aa2 天前
EasyExcel读和写
excel
冷咖啡离 我的笨笨2 天前
数据去重:通过 C# 删除 Excel 中的重复行
开发语言·c#·excel