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);
相关推荐
SunkingYang1 小时前
详细介绍C++中捕获异常类型的方式有哪些,分别用于哪些情形,哪些异常捕获可用于通过OLE操作excel异常
c++·excel·mfc·异常捕获·comerror
lied16636348067 小时前
List导出到Excel文件
servlet·list·excel
ZhengEnCi13 小时前
Excel 文件结构完全指南-从基础概念到 Python 读取的实用宝典
python·excel
核桃杏仁粉15 小时前
excel拼接数据库
数据库·oracle·excel
cx330上的猫15 小时前
价值1w的数据分析课知识点汇总-excel使用(第一篇)
数据挖掘·数据分析·excel
小薛引路19 小时前
office便捷办公06:根据相似度去掉excel中的重复行
windows·python·excel
CodeLongBear1 天前
苍穹外卖 Day12 实战总结:Apache POI 实现 Excel 报表导出全流程解析
java·excel
技术钱1 天前
vue3 封装图片上传预览组件支持docx、excel、pdf、图片、txt格式
vue.js·pdf·excel
r i c k1 天前
Excel表----VLOOKUP函数实现两表的姓名、身份证号码、银行卡号核对
excel
枫,为落叶2 天前
【vue】导出excel
前端·vue.js·excel