POI生成excel文件,自定义单元格颜色

摘要:
palette.setColorAtIndex;palette.setColorAtIndex;palette.setColorAtIndex;*/然后使用颜色。例如,可以使用新的颜色索引:样式替换原始颜色。setFillForegroundColor;3、 SetFillPattern(),设置单元格填充样式,例如样式。setFillPattern;style.setFillForegroundColor;style.setFillBackgroundColor;这样,当前单元格将用红色和蓝色交替网格中的三行代码填充,并删除setFillPattern设置填充样式的行,同时设置前景色和背景色。生成的文件不会填充颜色,因此此时不会填充前景颜色或背景颜色。

  一、先说设置单元格的背景颜色:
  HSSFWorkbook wb = new HSSFWorkbook();
  ...
  HSSFCellStyle style = wb.createCellStyle();
  style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
  style.setFillForegroundColor(HSSFColor.WHITE.index);
  cell.setCellStyle(style);  //cell 是 HSSFCell 对象
setFillPattern是设置单元格填充样式,SOLID_FOREGROUND纯色使用前景颜色填充,接着设置前景颜色(setFillForegroundColor)就可以给单元格着色了。setFillForegroundColor()方法的参数是一个short类型,POI使用索引来代表颜色,默认已经有一些颜色了,如:
    8: BLACK
    60: BROWN
    59: OLIVE_GREEN
    58: DARK_GREEN
    ... 
颜色的索引还必须是 0x08 ~ 0x40 (8 ~ 64) 的数字。

        二、接下来,使用自定义颜色
如果不使用POI提供的默认颜色,就需要自定颜色索引:
  HSSFPalette palette = wb.getCustomPalette();  //wb HSSFWorkbook对象
  palette.setColorAtIndex((short) 9, (byte) (color.getRed()), (byte) (color.getGreen()), (byte) (color.getBlue()));
   
   /*设置颜色的索引只能是 8 ~ 64,在此之外的索引无效,也不会报错。以下三种方式都可以设置成功。
   palette.setColorAtIndex((short)9, (byte) (0xff & 251), (byte) (0xff & 161), (byte) (0xff & 161));
   palette.setColorAtIndex((short)10, (byte) (0x66), (byte) (0xcd), (byte) (0xaa));
   palette.setColorAtIndex((short)11, (byte) (255), (byte) (165), (byte) (0));
   */
然后使用颜色,如上例,可以用新的颜色索引,替换原有的颜色:
  style.setFillForegroundColor((short) 9);

        三、setFillPattern(),设置单元格填充的样式,比如:
   style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
   style.setFillForegroundColor(HSSFColor.RED.index);
   style.setFillBackgroundColor(HSSFColor.LIGHT_BLUE.index);
这样当前单元格就被红蓝交替的格子填充

 POI生成excel文件,自定义单元格颜色 - ljhzzyx - 怀念外婆屋后的柚子树
上面3行代码,去掉setFillPattern设置填充样式的一行,同时设置前景色和背景色,生成的文件没有填充颜色,此时既不会用前景色填充,也不会用背景色填充。这种情况与 setFillPattern(HSSFCellStyle.NO_FILL); 时一样。
api上setFillBackgroundColor方法说明有如下示例:

public void setFillBackgroundColor(short bg)
set the background fill color.

For example:

 cs.setFillPattern(HSSFCellStyle.FINE_DOTS );
 cs.setFillBackgroundColor(new HSSFColor.RED().getIndex()); 
 //上面代码经测试,是黑色点状的背景(无前景),设置红色背景色无效
optionally a Foreground and background fill can be applied: Note: Ensure Foreground color is set prior to background
 cs.setFillPattern(HSSFCellStyle.FINE_DOTS );
 cs.setFillForegroundColor(new HSSFColor.BLUE().getIndex());
 cs.setFillBackgroundColor(new HSSFColor.RED().getIndex());
or, for the special case of SOLID_FILL:
 cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND );
 cs.setFillForegroundColor(new HSSFColor.RED().getIndex());
 
It is necessary to set the fill style in order for the color to be shown in the cell. 

免责声明:文章转载自《POI生成excel文件,自定义单元格颜色》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇influxDB在grafana中使用Flink初探-为什么选择Flink下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

PostgreSQL 那些值得尝试的功能,你知道多少?

原文:PostgreSQL Features You May Not Have Tried But Should 链接: https://pgdash.io/blog/postgres-features.html 译者:Rhys_Lee,xiaoaiwhc1,边城,ljwheyxy,lnovonl 审校:开源中国,转载请注明来源 PostgreSQL包...

(转)SqlServer为大数据量表建索引

本文转载自:http://blog.csdn.net/iangujun/article/details/8136764 之前从没有用SqlServer数据库处理过大数据量的表,都是用Oracle,然后一般为数据量较大的表添加索引或主键都是用plsql工具,今天正好需要为一张保存于SqlServer数据库的千万级数据表增加索引,于是遇到了下面一系列的问题。...

MySQL-添加索引或字段时如何不锁表

索引的添加MySQL经历了一下几个历程: 一 .在MySQL 5.5版本之前,添加索引具体是这样的: 1.首先创建一张临时表和原表数据结构相同,将你要添加的索引加上。 2.把原表数据导入临时表。 3.删除原表。 4.将临时表重命名为原表。 这样做有很大问题: 首先对于大数据量的导入需要很长的时间,那么在这段时间里新增或修改的数据没办法处理。 其次如果碰上大...

【POI】修改Excel内容

    1 package com.what21.test; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream;...

Java电商支付系统手把手实现(二)

1 数据库设计 1.1 表关系梳理 仔细思考业务关系,得到如下表关系图 1.2 用户表结构 1.3 分类表结构 id=0为根节点,分类其实是树状结构 1.4 商品表结构 注意价格字段的类型为 decimal 1.5 支付信息表结构 1.6 订单表结构 乍一看,有必要搞这么多种的时间嘛?有以下诸多原因 前端显示需要,那就必须存着呀! 方便定位排...

Excel.Application手册

----转载:http://blog.csdn.net/xxfigo/article/details/6618129 定制模块行为(1) Option Explicit '强制对模块内所有变量进行声明    Option Private Module '标记模块为私有,仅对同一工程中其它模块有用,在宏对话框中不显示    Option Compare T...