[英]Setting background custom color, XSSFWorkbook
I use this code to change the font in excel to color, which I define 我使用此代码将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);
Can I change the cell's Background same way. 我可以用相同的方式更改单元格的背景吗?
I saw the question here Setting background custom color not working for XSSF in Apache POI And I used the code: 我在这里看到了在Apache POI中设置背景自定义颜色不适用于XSSF的问题,并且使用了以下代码:
XSSFCellStyle cellStyle = wb.createCellStyle();
XSSFColor color = new XSSFColor(new java.awt.Color(value, 0, 0));
((XSSFCellStyle)cellStyle).setFillBackgroundColor(color);
cell.setCellStyle(cellStyle);
The background still always white. 背景仍然总是白色。
Am sure that all other parts of the code written properly, because it works when I changed the font. 确保代码的所有其他部分都正确编写,因为更改字体后它可以工作。
I have office 2010 on my computer 我的计算机上装有Office 2010
Thanks all I found solution now 谢谢所有我现在找到解决方案
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);
Create cell style object: 创建单元格样式对象:
CellStyle backgroundStyle = workbook.createCellStyle();
Set custom color: 设置自定义颜色:
backgroundStyle.setFillForegroundColor(IndexedColors.YELLOW.getIndex());
Add style to the cell: 为单元格添加样式:
cell.setCellStyle(backgroundStyle);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.