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 小时前
【wps】 excel 删除重复项
excel·wps
盛夏绽放9 小时前
Vue3 中 Excel 导出的性能优化与实战指南
vue.js·excel
Tomorrow'sThinker12 小时前
[特殊字符] Python 自动查找替换 Excel 单元格内容 —— 高效批量处理
excel
Shipley Leo12 小时前
如何在Excel中每隔几行取一行
excel
bing_15812 小时前
Excel 数据透视表不够用时,如何处理来自多个数据源的数据?
excel
想要入门的程序猿16 小时前
Qt写入excel
数据库·qt·excel
爱代码的小黄人1 天前
Excel VLOOKUP函数使用详解:原理、格式、常见错误与解决方案
excel
YouYOUyouFairy1 天前
EXCEL动态表格
excel
Maruko3102 天前
Java 实现excel大批量导出
excel·poi
忧郁的蛋~2 天前
HTML表格导出为Excel文件的实现方案
前端·html·excel