我使用此代码将 Excel 中的字体更改为我定义的颜色
Color sColor = new Color (value,0,0);
XSSFColor userColor = new XSSFColor(sColor);
CellStyle style = wb.createCellStyle();
XSSFFont font = wb.createFont();
font.setColor(userColor);
style.setFont(font);
cell.setCellStyle(style);
我可以用同样的方式更改单元格的背景吗?
我在这里看到了这个问题 Setting background custom color not working for XSSF in Apache POI 我使用了代码:
XSSFCellStyle cellStyle = wb.createCellStyle();
XSSFColor color = new XSSFColor(new java.awt.Color(value, 0, 0));
((XSSFCellStyle)cellStyle).setFillBackgroundColor(color);
cell.setCellStyle(cellStyle);
背景始终为白色。
我确信代码的所有其他部分都正确编写,因为当我更改字体时它可以工作。
我的计算机上有 Office 2010
最佳答案
谢谢大家 我现在找到了解决方案
XSSFCellStyle cellStyle = wb.createCellStyle();
XSSFColor color = new XSSFColor(new java.awt.Color(value, 0, 0));
((XSSFCellStyle)cellStyle).setFillBackgroundColor(color);
cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cellStyle.setFillForegroundColor(color);
cell.setCellStyle(cellStyle);
关于java - 设置背景自定义颜色,XSSFWorkbook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53197569/