问题:
某些excel中单元格的背景填充色的rgb值无法获取到
检查分析:
通过查看excel的xml文件得知,单元格的填充背景色分为两种:
1、用户自定义的颜色
2、excel的主题的颜色
解决方案:
所以获取excel单元格背景填充色有两个方法分别如下:
1、获取存储单元格填充背景色的XSSFColor对象
XSSFColor fillForegroundColorColor = cell.getCellStyle().getFillForegroundColorColor();
2、获取单元格背景填充色 主题颜色rgb值方法
byte[] bytes = fillForegroundColorColor.getRGBWithTint();
3、获取单元格填充背景色rgb值方法
byte[] bytes = fillForegroundColorColor.getRGB();
// 填充背景填充颜色
XSSFColor fillForegroundColorColor = (XSSFColor) cell.getCellStyle().getFillForegroundColorColor();
if (null != fillForegroundColorColor) {
byte[] bytes;
// 首先获取excel中主题模板填充颜色rgb
bytes = fillForegroundColorColor.getRGBWithTint();
// 获取为空获取填充颜色rgb
if (null == bytes){
bytes = fillForegroundColorColor.getRGB();
}
// rgb数组不为空word 单元格 填充色复制
if (null != bytes){
ctPr.addNewShd().setFill(String.format("%02X%02X%02X", bytes[0], bytes[1], bytes[2]));
}
}