The method setFillForegroundColor(short) in the type CellStyle is not applicable for the arguments (XSSFColor)

雨燕双飞 提交于 2021-02-08 07:52:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!