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设置单元格背景色相关推荐

  1. POI 导出 excel 设置单元格背景色 的坑

    1.必须调用设置前景色的方法来设置背景色: 2.必须调用 setFillPattern(): 3.代码如下: //cl.setFillBackgroundColor(IndexedColors.YEL ...

  2. java导出excel表格设置行高,POI导出Excel设置单元格格式

    使用Apache的POI相关API导出Excel设置单元格格式 栗子,一下各个代码之间的变量是通用的,要是在某个代码块中找不到某个变量,则可以向上找寻 准备工作 InputStream = templ ...

  3. POI导出——Excel实现单元格的背景色填充

    1.背景 随着业务需求的扩充,简简单单的Excel导出已经不能满足客户的胃口了.而POI api这个家伙里面的坑有时候真的是让你分分钟没有脾气,所以打算记录下来,分享一下poi的坑及其解决方法. 2. ...

  4. java poi设置单元格格式为数值_java中导出excel设置单元格的样式为数字格式怎么设置_Java代码实现设置单元格格式...

    java中导出excel设置单元格的样式为数字格式怎么设置 如果是使用poi,它会自动根据参数值设置单元格为恰当格式,只需传入数字类型参数值即可,比如 double value=....; cell. ...

  5. java-EasyExcel导出excel设置单元格为文本格式(含代码)

    java-EasyExcel导出excel设置单元格为文本格式(含代码) 在使用EasyExcel导出excel模板时.我们会发现导出的日期和大长度数字都会自动更换格式,不是文本格式.并且在空白单元格 ...

  6. java poi excel 单元格样式_java poi批量导出excel 设置单元格样式

    POI中可能会用到一些需要设置EXCEL单元格格式的操作小结: 先获取工作薄对象: HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb ...

  7. NPOI之Excel——设置单元格背景色

    NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置,FillP ...

  8. java使用POI导出Excel设置单元格格式为数值类型

    最近做项目遇到的坑,百度了半天导出都为货币类型.自定义类型和常规类型,,,最后终于解决,在此记录一下 其中contextstyle.setDataFormat(df.getFormat("0 ...

  9. hutool导出excel 设置单元格日期格式 poi设置excel单元格日期格式

    hutool导出导入excel很方便,但没有依赖poi,需要手动添加poi依赖. <properties><poi.version>4.1.2</poi.version& ...

最新文章

  1. 鸿蒙初开踏青时主要内容,袖珍世界之鸿蒙初开
  2. 架构模式: 事务日志跟踪
  3. Django目录结构说明
  4. static和不完全类型的一个例子
  5. gradle 项目打包成多个jar包_永不失优雅——高效管理Springboot项目
  6. ajax和php没反应,PHP和AJAX没有更新发生
  7. H264--1--编码原理以及I帧B帧P帧、ptsdts
  8. python_PDF合成软件_ZHOU125disorder_
  9. 2022年4月30号Mysql语句增删改查(CRUB)重在实操。
  10. 怎样高效地自学软件测试
  11. Java中IO演练之银行账号校验
  12. 交换机:广播风暴产生原因与解决方法、STP生成树协议 ,根桥(根交换机)、备份根桥、非根交换机、根端口、指定端口、非根非指定端口、桥ID
  13. LeetCode 714. 买卖股票的最佳时机含手续费--动态规划
  14. MSP430F149的TIMERA定时中断理解
  15. catic备份mysql,Catic构建与部署
  16. 全屏背景图移动端滚动时白底问题
  17. AD18导出BOM清单报错
  18. 随着新日益增多的技术,如何正确的提升自己?
  19. 解决斜杠符号被转义的问题
  20. 客户端开发GUI框架对比与技术选型总结

热门文章

  1. Matlab中ylim函数的使用
  2. 分数的四则运算及化简(C语言实现)
  3. html 盒子模型添加图片,[HTML/CSS]盒子模型,块级元素和行内元素
  4. 【OpenGL】实现三维空间漫游和立方体、球体贴图
  5. 脑波震动(一):头部脑波震动
  6. iview upload组件手动控制上传,excel表格导入
  7. matlab 版 数独小游戏 GUI界面设计
  8. 美容美发商家的痛点说明
  9. w7 w10 如何开启远程桌面简易版
  10. Oracle之Check约束实例详解