easyExcel自定义背景颜色easyPoi自定义修改表头背景色 |
您所在的位置:网站首页 › 通达信自定义颜色对照表 › easyExcel自定义背景颜色easyPoi自定义修改表头背景色 |
背景:Excel自定义背景颜色,自定义表头背景颜色 注意点:单元格设置背景颜色失效原因/不生效的原因,通常是因为只设置了setFillForegroundColor 参数,这样肯定是不行的,还需要再设置一下 setFillPattern,在setFillPattern中添加FillPatternType.SOLID_FOREGROUND即可 cellStyle.setFillForegroundColor(****) cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);方法1 - 使用默认的自定义颜色: cellStyle.setFillForegroundColor(IndexedColors.LIGHT_TURQUOISE.getIndex()); cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);通过获取IndexedColors的不同值来更改颜色(默认颜色对照表在文章底部) 方法2 - 使用rgb方法自定义 : /** * 标题背景色 */ private static final String DEFAULT_BACKGROUND_COLOR = "#9BC2E6"; // 自定义背景色 int r = Integer.parseInt((DEFAULT_BACKGROUND_COLOR.substring(1,3)),16); int g = Integer.parseInt((DEFAULT_BACKGROUND_COLOR.substring(3,5)),16); int b = Integer.parseInt((DEFAULT_BACKGROUND_COLOR.substring(5,7)),16); HSSFWorkbook wb = new HSSFWorkbook(); HSSFPalette palette = wb.getCustomPalette(); HSSFColor hssfColor = palette.findSimilarColor(r, g, b); // 背景色与填充 cellStyle.setFillForegroundColor(hssfColor.getIndex()); cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);默认颜色对照表(根据第1列下标对照颜色): 以上则是IndexedColors所支持的所有默认颜色,如果默认颜色没有你想要的,或者产品指定颜色了,那你需要使用第二种rgb的方式去自定义颜色。 扩展:第二种rgb的方式自定义颜色使用的是HSSFColor,HSSFColor跟IndexedColors一样都是本身支持的一些默认颜色,代码过程就是使用16进制根据下标切割 #9BC2E6 ,再进行转换添加到HSSFColor默认颜色中 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |