Java 将单元格颜色更改为特定的定义颜色
我想将单元格的颜色更改为我用java.awt.color[r=255,g=255,b=255]定义的颜色 我使用了代码:Java 将单元格颜色更改为特定的定义颜色,java,excel,Java,Excel,我想将单元格的颜色更改为我用java.awt.color[r=255,g=255,b=255]定义的颜色 我使用了代码: Color sColor = new Color (200,0,0); XSSFColor userColor = new XSSFColor(sColor); try { CellStyle style = wb.createCellStyle(); Font font = wb.crea
Color sColor = new Color (200,0,0);
XSSFColor userColor = new XSSFColor(sColor);
try {
CellStyle style = wb.createCellStyle();
Font font = wb.createFont();
font.setColor(userColor.getIndexed());
style.setFont(font);
cell.setCellStyle(style);
} catch (NumberFormatException | NullPointerException ex) {
//Handle NumberFormat and NullPointer exceptions here
}
颜色始终保持黑色
“System.out.print(userColor);”的输出为:
(org.apache.poi.xssf.usermodel。XSSFColor@2e1b928).
实际上,它的变化取决于我在“新颜色(200,0,0);”中键入的值
但是“System.out.print(userColor.getIndexed());”的输出总是:(0)
有什么提示吗?原因是
sColor
是您自己定义的,所以没有相关的索引
font.setColor(userColor.getIndexed());//this line of code will not work
检查的API,没有使用自定义颜色设置字体的方法,只能使用默认的
因此,在POI升级以支持它之前,您需要更改您的设计
谢谢您的回答,您有没有办法定义背景颜色而不是字体。实际上,我的主要目标是根据细胞的价值来区分细胞。“excel中有一个叫做热图的工具,我想调用它。”@lucumt