Java 为什么Apache POI无法使用rgb获取我选择的颜色
我用下面的方法用rgb获得颜色Java 为什么Apache POI无法使用rgb获取我选择的颜色,java,apache-poi,Java,Apache Poi,我用下面的方法用rgb获得颜色 public short getRGBColor(int red, int green, int blue){ HSSFWorkbook hwb = new HSSFWorkbook(); HSSFPalette palette = hwb.getCustomPalette(); return palette.findSimilarColor(red,green, blue).getIndex(); } 然
public short getRGBColor(int red, int green, int blue){
HSSFWorkbook hwb = new HSSFWorkbook();
HSSFPalette palette = hwb.getCustomPalette();
return palette.findSimilarColor(red,green, blue).getIndex();
}
然后调用下面的方法
public CellStyle createStyles(Workbook wb){
CellStyle style;
Font font1 = wb.createFont();
font1.setBold(false);
style = wb.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setFillForegroundColor(getRGBColor(211,225,237));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setFont(font1);
return style;
}
这是我选择的颜色代码211225237
但是getRGBColor方法返回另一个颜色代码204255
正如您在中所看到的,方法
查找SimilarColor
在自定义调色板中查找最匹配的颜色。您可以尝试使用方法findColor
获取准确的颜色。但是请注意,方法findColor
可以返回null
根据文档,setFillForegroundColor
的参数可以是枚举IndexedColors
或setFillBackgroundColor(XSSFColor color)
中的索引值
尝试使用XSSFColor类中的setRGB
样本代码-
headerCellStyle.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());