POI设置Excel单元格背景色(setFillForegroundColor与setFillPattern的使用)

转自:https://blog.csdn.net/qq_38025219/article/details/82760471

 

背景:

使用Java开发信息系统项目,项目中往往会涉及到报表管理部分,而Excel表格首当其冲称为最合适的选择,

但是对单元格操作时对于设置单元格的背景颜色却很少提及,本文旨在方便单元格背景颜色设计。

 

操作:

至于冗长的创建表格表格设置的代码相信大家都已经了解。直接进行单元格背景颜色设计。

复制代码
// 创建一个 workbook 对象 
Workbook workbook = new XSSFWorkbook();
// 创建一个 sheet
Sheet sheet = workbook.createSheet();
//创建一行
Row row = sheet.createRow((short) 1);
ellStyle style = workbook.createCellStyle();
//关键点 IndexedColors.AQUA.getIndex() 对应颜色
style.setFillForegroundColor(***IndexedColors.AQUA.getIndex()***);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);
Cell cell = row.createCell((short) 1);
cell.setCellValue("X1");
cell.setCellStyle(style);
复制代码

 

颜色与代码参考:

在这里插入图片描述

上面的单元格颜色对应下面的英语颜色表示,从X1-X49 按顺序对应;

将下面对应的code填入上述代码加粗斜体位置即可。

复制代码
IndexedColors.AQUA.getIndex() //1
IndexedColors.AUTOMATIC.getIndex() //2
IndexedColors.BLUE.getIndex() //3
IndexedColors.BLUE_GREY.getIndex() //4
IndexedColors.BRIGHT_GREEN.getIndex() //5
IndexedColors.BROWN.getIndex() //6
IndexedColors.CORAL.getIndex() //7
IndexedColors.CORNFLOWER_BLUE.getIndex() //8
IndexedColors.DARK_BLUE.getIndex() //9
IndexedColors.DARK_GREEN.getIndex() //10
IndexedColors.DARK_RED.getIndex() //11
IndexedColors.DARK_TEAL.getIndex() //12
IndexedColors.DARK_YELLOW.getIndex() //13
IndexedColors.GOLD.getIndex() //14
IndexedColors.GREEN.getIndex() //15
IndexedColors.GREY_25_PERCENT.getIndex() //16
IndexedColors.GREY_40_PERCENT.getIndex() //17
IndexedColors.GREY_50_PERCENT.getIndex() //18
IndexedColors.GREY_80_PERCENT.getIndex() //19
IndexedColors.INDIGO.getIndex() //20
IndexedColors.LAVENDER.getIndex() //21
IndexedColors.LEMON_CHIFFON.getIndex() //22
IndexedColors.LIGHT_BLUE.getIndex() //23
IndexedColors.LEMON_CHIFFON.getIndex() //24
IndexedColors.LIGHT_BLUE.getIndex() //25
IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex() //26
IndexedColors.LIGHT_GREEN.getIndex() //27
IndexedColors.LIGHT_ORANGE.getIndex() //28
IndexedColors.LIGHT_TURQUOISE.getIndex() //29
IndexedColors.LIGHT_YELLOW.getIndex() //30
IndexedColors.LIME.getIndex() //31
IndexedColors.MAROON.getIndex() //32
IndexedColors.OLIVE_GREEN.getIndex() //33
IndexedColors.ORANGE.getIndex() //34
IndexedColors.ORCHID.getIndex() //35
IndexedColors.PALE_BLUE.getIndex() //36
IndexedColors.PINK.getIndex() //37
IndexedColors.PLUM.getIndex() //38
IndexedColors.RED.getIndex() //39
IndexedColors.ROSE.getIndex() //40
IndexedColors.ROYAL_BLUE.getIndex() //41 IndexedColors.SEA_GREEN.getIndex() //42 IndexedColors.SKY_BLUE.getIndex() //43 IndexedColors.TAN.getIndex() //44 IndexedColors.TEAL.getIndex() //45 IndexedColors.TURQUOISE.getIndex() //46 IndexedColors.VIOLET.getIndex() //47 IndexedColors.WHITE.getIndex() //48 IndexedColors.YELLOW.getIndex() //49
复制代码

 

posted @   大墨垂杨  阅读(8918)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示