POI 使用颜色字符串生成XSSFColor对象

时间:2022-12-07 14:49:20

public static int[] hexToRgb(String hex) {
String colorStr = hex;
if (hex.startsWith("#")) {
colorStr = hex.substring(1);
}
if (StringUtils.length(colorStr) == 8) {
colorStr = hex.substring(2);
}
int r= Integer.valueOf( colorStr.substring( 0, 2 ), 16 );
int g= Integer.valueOf( colorStr.substring( 2, 4 ), 16 );
int b= Integer.valueOf( colorStr.substring( 4, 6 ), 16 ); return new int[] { r, g, b };
}
private XSSFColor createXssfColor(String color) {
int[] rgbColor = hexToRgb(color);
XSSFColor xssfColor = new XSSFColor(new java.awt.Color(rgbColor[0], rgbColor[1], rgbColor[2]), new DefaultIndexedColorMap());
return xssfColor;
}