问题
I am trying to get custom colours to work in Apache POI but I got into a small problem. So far I have been using IndexedColors
, but as a palette, it is pretty drab. Thus using an RGB format for colour selection would help me to make my spreadsheets look much better.
I have been using this question's answer, as it solves my problem theoretically. However, I am facing a very silly problem that I do not really know how to solve.
The error I receive is:
The method setFillForegroundColor(short) in the type CellStyle is not applicable for the arguments (XSSFColor)
This should not really be happening, as I have seen it being used in many other examples and documentation about Apache POI. The IDE (Eclipse) suggests changing setFillForegroundColor
to setFillBackgroundColor
but doing this, only makes the idea suggest the vice versa as a solution. What a joker can my IDE be...
I have created a Map
for my styles and that could be somehow convoluting everything but I do not really believe that this is the issue. Or could it?
What can I do to solve this nonsense?
Code excerpt:
private static Map<String, CellStyle> styling(Workbook wb)
{
Map<String, CellStyle> styles = new HashMap<>();
CellStyle style;
Font font1 = wb.createFont();
font1.setBold(true);
style = createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.RIGHT);
style.setFont(font1);
style.setFillBackgroundColor(new XSSFColor(new java.awt.Color(255, 255, 0), new DefaultIndexedColorMap()));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
styles.put("column_headers", style);
return styles;
}
Some visual comedy
setFillBackgroundColor
setFillForegroundColor
回答1:
Thanks to Axel Richter I solved the issue. The problem is that I should have sticked to the XSSF libraries.
private static Map<String, CellStyle> styling(Workbook wb)
{
Map<String, CellStyle> styles = new HashMap<>();
XSSFCellStyle style;
Font font1 = wb.createFont();
font1.setBold(true);
style = (XSSFCellStyle) createBorderedStyle(wb);
style.setAlignment(HorizontalAlignment.RIGHT);
style.setFont(font1);
style.setFillForegroundColor(new XSSFColor(new java.awt.Color(128, 0, 128), new DefaultIndexedColorMap()));
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
styles.put("cell_g", style);
return styles;
}
来源:https://stackoverflow.com/questions/58952888/the-method-setfillforegroundcolorshort-in-the-type-cellstyle-is-not-applicable