POI导出Excel设置单元格背景色
POI导出Excel设置单元格背景色
导出Excel的时候,没有设置背景色,用2003版本的Excel工具打开会出现文档单元格背景自动填充黑色的情况,没有找到好的解决方法,就主动给他填充一种颜色,问题就算解决了。在WPS中似乎会自动将黑色改为白色,所以我用WPS看不到填充黑色的情况。
This example shows you Excel cell fills and colors using Apache POI.In our example i have used all the possible colors and set it as Fills background colors of cells.Below is the example code.package com.java.connect.poi;import java.io.FileOutputStream;
import java.io.IOException;import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;public class POIFillAndColorExample {public static void main(String[] args) throws IOException {// Create a workbook objectWorkbook workbook = new XSSFWorkbook();// Create sheetSheet sheet = workbook.createSheet();// Create a row and put some cells in itRow row = sheet.createRow((short) 1);// Aqua backgroundCellStyle style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.AQUA.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);Cell cell = row.createCell((short) 1);cell.setCellValue("X1");cell.setCellStyle(style);// Orange "foreground", foreground being the fill foreground not the// font color.style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.AUTOMATIC.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row.createCell((short) 2);cell.setCellValue("X2");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.BLUE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row.createCell((short) 3);cell.setCellValue("X3");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.BLUE_GREY.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row.createCell((short) 4);cell.setCellValue("X4");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.BRIGHT_GREEN.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row.createCell((short) 5);cell.setCellValue("X5");cell.setCellStyle(style);// Create a row and put some cells in it.Row row2 = sheet.createRow((short) 2);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.BROWN.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row2.createCell((short) 1);cell.setCellValue("X6");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.CORAL.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row2.createCell((short) 2);cell.setCellValue("X7");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.CORNFLOWER_BLUE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row2.createCell((short) 3);cell.setCellValue("X8");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row2.createCell((short) 4);cell.setCellValue("X9");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.DARK_GREEN.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row2.createCell((short) 5);cell.setCellValue("X10");cell.setCellStyle(style);// Create a row and put some cells in it.Row row3 = sheet.createRow((short) 3);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.DARK_RED.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row3.createCell((short) 1);cell.setCellValue("X11");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.DARK_TEAL.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row3.createCell((short) 2);cell.setCellValue("X12");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.DARK_YELLOW.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row3.createCell((short) 3);cell.setCellValue("X13");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.GOLD.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row3.createCell((short) 4);cell.setCellValue("X14");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.GREEN.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row3.createCell((short) 5);cell.setCellValue("X15");cell.setCellStyle(style);// Create a row and put some cells in it.Row row4 = sheet.createRow((short) 4);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row4.createCell((short) 1);cell.setCellValue("X16");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row4.createCell((short) 2);cell.setCellValue("X17");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row4.createCell((short) 3);cell.setCellValue("X18");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.GREY_80_PERCENT.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row4.createCell((short) 4);cell.setCellValue("X19");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.INDIGO.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row4.createCell((short) 5);cell.setCellValue("X20");cell.setCellStyle(style);// Create a row and put some cells in it.Row row5 = sheet.createRow((short) 5);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.LAVENDER.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row5.createCell((short) 1);cell.setCellValue("X21");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row5.createCell((short) 2);cell.setCellValue("X22");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row5.createCell((short) 3);cell.setCellValue("X23");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.LEMON_CHIFFON.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row5.createCell((short) 4);cell.setCellValue("X24");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row5.createCell((short) 5);cell.setCellValue("X25");cell.setCellStyle(style);// Create a row and put some cells in it.Row row6 = sheet.createRow((short) 6);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row6.createCell((short) 1);cell.setCellValue("X26");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.LIGHT_GREEN.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row6.createCell((short) 2);cell.setCellValue("X27");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.LIGHT_ORANGE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row6.createCell((short) 3);cell.setCellValue("X28");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row6.createCell((short) 4);cell.setCellValue("X29");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.LIGHT_YELLOW.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row6.createCell((short) 5);cell.setCellValue("X30");cell.setCellStyle(style);// Create a row and put some cells in it.Row row7 = sheet.createRow((short) 7);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.LIME.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row7.createCell((short) 1);cell.setCellValue("X31");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.MAROON.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row7.createCell((short) 2);cell.setCellValue("X32");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.OLIVE_GREEN.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row7.createCell((short) 3);cell.setCellValue("X33");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row7.createCell((short) 4);cell.setCellValue("X34");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.ORCHID.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row7.createCell((short) 5);cell.setCellValue("X35");cell.setCellStyle(style);// Create a row and put some cells in it.Row row8 = sheet.createRow((short) 8);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.PALE_BLUE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row8.createCell((short) 1);cell.setCellValue("X36");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.PINK.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row8.createCell((short) 2);cell.setCellValue("X37");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.PLUM.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row8.createCell((short) 3);cell.setCellValue("X38");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.RED.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row8.createCell((short) 4);cell.setCellValue("X39");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.ROSE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row8.createCell((short) 5);cell.setCellValue("X40");cell.setCellStyle(style);// Create a row and put some cells in it.Row row9 = sheet.createRow((short) 9);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.ROYAL_BLUE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row9.createCell((short) 1);cell.setCellValue("X41");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.SEA_GREEN.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row9.createCell((short) 2);cell.setCellValue("X42");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.SKY_BLUE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row9.createCell((short) 3);cell.setCellValue("X43");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.TAN.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row9.createCell((short) 4);cell.setCellValue("X44");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.TEAL.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row9.createCell((short) 5);cell.setCellValue("X45");cell.setCellStyle(style);// Create a row and put some cells in it.Row row10 = sheet.createRow((short) 10);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.TURQUOISE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row10.createCell((short) 1);cell.setCellValue("X46");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.VIOLET.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row10.createCell((short) 2);cell.setCellValue("X47");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.WHITE.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row10.createCell((short) 3);cell.setCellValue("X48");cell.setCellStyle(style);style = workbook.createCellStyle();style.setFillForegroundColor(IndexedColors.YELLOW.getIndex());style.setFillPattern(CellStyle.SOLID_FOREGROUND);cell = row10.createCell((short) 3);cell.setCellValue("X49");cell.setCellStyle(style);// Write the output to a fileFileOutputStream fileOut = new FileOutputStream("POIFillAndColorExample.xlsx");workbook.write(fileOut);fileOut.close();}
}
如颜色类型是在HSSFColor里面定义的.执行结果暂时没放上来,后来再补充。
颜色 | 执行结果 |
---|---|
HSSFColor.ROYAL_BLUE | |
HSSFColor.TEAL | |
HSSFColor.LIME | |
HSSFColor.PALE_BLUE | |
HSSFColor.AQUA | |
HSSFColor.GREEN | |
HSSFColor.TURQUOISE | |
HSSFColor.DARK_BLUE | |
HSSFColor.CORNFLOWER_BLUE | |
HSSFColor.OLIVE_GREEN | |
HSSFColor.WHITE | |
HSSFColor.LIGHT_TURQUOISE | |
HSSFColor.LEMON_CHIFFON | |
HSSFColor.LIGHT_GREEN | |
HSSFColor.BLUE | |
HSSFColor.DARK_RED | |
HSSFColor.CORAL | |
HSSFColor.RED | |
HSSFColor.LIGHT_YELLOW | |
HSSFColor.SKY_BLUE | |
HSSFColor.BROWN | |
HSSFColor.SEA_GREEN | |
HSSFColor.INDIGO | |
HSSFColor.MAROON | |
HSSFColor.GREY_80_PERCENT | |
HSSFColor.GREY_25_PERCENT | |
HSSFColor.DARK_GREEN | |
HSSFColor.YELLOW | |
HSSFColor.GOLD | |
HSSFColor.GREY_40_PERCENT | |
HSSFColor.DARK_TEAL | |
HSSFColor.PINK | |
HSSFColor.ORCHID | |
HSSFColor.LIGHT_BLUE | |
HSSFColor.LIGHT_CORNFLOWER_BLUE | |
HSSFColor.BLACK | |
HSSFColor.DARK_YELLOW | |
HSSFColor.VIOLET | |
HSSFColor.LAVENDER | |
HSSFColor.ROSE | |
HSSFColor.BLUE_GREY | |
HSSFColor.LIGHT_ORANGE | |
HSSFColor.ORANGE | |
HSSFColor.GREY_50_PERCENT |
注意:
(1)必须调用设置前景色的方法来设置背景色;
(2)必须调用setFillPattern()方法设置全填充。
POI导出Excel设置单元格背景色相关推荐
- POI 导出 excel 设置单元格背景色 的坑
1.必须调用设置前景色的方法来设置背景色: 2.必须调用 setFillPattern(): 3.代码如下: //cl.setFillBackgroundColor(IndexedColors.YEL ...
- java导出excel表格设置行高,POI导出Excel设置单元格格式
使用Apache的POI相关API导出Excel设置单元格格式 栗子,一下各个代码之间的变量是通用的,要是在某个代码块中找不到某个变量,则可以向上找寻 准备工作 InputStream = templ ...
- POI导出——Excel实现单元格的背景色填充
1.背景 随着业务需求的扩充,简简单单的Excel导出已经不能满足客户的胃口了.而POI api这个家伙里面的坑有时候真的是让你分分钟没有脾气,所以打算记录下来,分享一下poi的坑及其解决方法. 2. ...
- java poi设置单元格格式为数值_java中导出excel设置单元格的样式为数字格式怎么设置_Java代码实现设置单元格格式...
java中导出excel设置单元格的样式为数字格式怎么设置 如果是使用poi,它会自动根据参数值设置单元格为恰当格式,只需传入数字类型参数值即可,比如 double value=....; cell. ...
- java-EasyExcel导出excel设置单元格为文本格式(含代码)
java-EasyExcel导出excel设置单元格为文本格式(含代码) 在使用EasyExcel导出excel模板时.我们会发现导出的日期和大长度数字都会自动更换格式,不是文本格式.并且在空白单元格 ...
- java poi excel 单元格样式_java poi批量导出excel 设置单元格样式
POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb ...
- NPOI之Excel——设置单元格背景色
NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置,FillP ...
- java使用POI导出Excel设置单元格格式为数值类型
最近做项目遇到的坑,百度了半天导出都为货币类型.自定义类型和常规类型,,,最后终于解决,在此记录一下 其中contextstyle.setDataFormat(df.getFormat("0 ...
- hutool导出excel 设置单元格日期格式 poi设置excel单元格日期格式
hutool导出导入excel很方便,但没有依赖poi,需要手动添加poi依赖. <properties><poi.version>4.1.2</poi.version& ...
最新文章
- 鸿蒙初开踏青时主要内容,袖珍世界之鸿蒙初开
- 架构模式: 事务日志跟踪
- Django目录结构说明
- static和不完全类型的一个例子
- gradle 项目打包成多个jar包_永不失优雅——高效管理Springboot项目
- ajax和php没反应,PHP和AJAX没有更新发生
- H264--1--编码原理以及I帧B帧P帧、ptsdts
- python_PDF合成软件_ZHOU125disorder_
- 2022年4月30号Mysql语句增删改查(CRUB)重在实操。
- 怎样高效地自学软件测试
- Java中IO演练之银行账号校验
- 交换机:广播风暴产生原因与解决方法、STP生成树协议 ,根桥(根交换机)、备份根桥、非根交换机、根端口、指定端口、非根非指定端口、桥ID
- LeetCode 714. 买卖股票的最佳时机含手续费--动态规划
- MSP430F149的TIMERA定时中断理解
- catic备份mysql,Catic构建与部署
- 全屏背景图移动端滚动时白底问题
- AD18导出BOM清单报错
- 随着新日益增多的技术,如何正确的提升自己?
- 解决斜杠符号被转义的问题
- 客户端开发GUI框架对比与技术选型总结