1

I've found the following question which is similar but not exactly the same:

Setting foreground color for HSSFCellStyle is always coming out black

So I'm creating an XSSFCellStyle for the headers of my table, and giving them a Foreground color with the following:

    wb = new XSSFWorkbook();
    headerStyle = wb.createCellStyle();
    XSSFColor headerBackgroundColor = new XSSFColor(new java.awt.Color(187,187,187));
    headerStyle.setFillForegroundColor( headerBackgroundColor);
    headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

Then I apply it to a cell using the setCellStyle.

Everything there works fine until I try adding a border around that/those cells.

I'm using propertyTemplate.drawBorders(range, borderType, color, extent);

    CellRangeAddress range = new CellRangeAddress(startRow, endRow, startColumn, endColumn);
    BorderStyle borderType = BorderStyle.MEDIUM;
    short color = IndexedColors.BLACK.getIndex();
    BorderExtent extent = BorderExtent.ALL;
    propertyTemplate.drawBorders(range, borderType, color, extent);

When I apply this and generate my sheet, the cells that were styled with the headerStyle are completely black. I can change the text color to white and I can see all the data is still there.

When I comment out the setFillPattern in the headerStyle, I can see the borders properly, but then the foreground color is white as though the setFillForegroundColor was no set (even though it is).

I've tried using setFillBackgroundColor as well but I get the same results.

Does anyone have any idea where I might be going wrong or is this a known (or unknown?) bug?

Thank you!

Alex

Alex Riccio
  • 45
  • 1
  • 12
  • https://stackoverflow.com/questions/43959440/apache-poi-fills-xssf-cell-with-black-instead-of-desired-custom-color-when-apply – Axel Richter Jun 21 '17 at 18:06
  • Thank you Axel again! I was able to just change my colors to short and it worked as you described. Hopefully they will be able to update the library to accept a Color instead of a Short so I can use a wider pallette of Colors. – Alex Riccio Jun 22 '17 at 11:55

1 Answers1

0

I have used NPOI for XSSF which is for .net . You'll have translate to get equivalent code for Java.

 XSSFFont myFont = (XSSFFont)workbook.CreateFont();
 myFont.FontHeightInPoints = (short)10;
 myFont.FontName = "Arial";
 myFont.Color = IndexedColors.Black.Index;
 myFont.IsBold = false;
 myFont.IsItalic = false;

XSSFCellStyle myCellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
myCellStyle.FillBackgroundColor = IndexedColors.LightYellow.Index;
//myCellStyle.FillPattern = FillPattern.NoFill;
myCellStyle.FillForegroundColor = IndexedColors.LightTurquoise.Index;
myCellStyle.FillPattern = FillPattern.SolidForeground;
myCellStyle.Alignment = HorizontalAlignment.Left;
myCellStyle.VerticalAlignment = VerticalAlignment.Top;
myCellStyle.BorderBottom = BorderStyle.Thin;
myCellStyle.BorderTop = BorderStyle.Thin;
myCellStyle.BorderLeft = BorderStyle.Thin;
myCellStyle.BorderRight = BorderStyle.Thin;
myCellStyle.SetFont(defaultFont);
kumar chandraketu
  • 2,232
  • 2
  • 20
  • 25
  • Thank you kumar. This seems to be the only workaround until the setFillForegroundColor(Color color) is updated instead of using short color. It seems like a pretty big issue unless everyone is happy using the preset colors? – Alex Riccio Aug 25 '17 at 15:17