1
votes

I've tried different solutions posted on stack overflow to apply a background color to an Apache POI generated cell, but nothing worked.

I'm doing something like:

Workbook workbook = new XSSFWorkbook(); 
Sheet sheet = workbook.createSheet(sheetName);

XSSFCellStyle cellStyle = ((XSSFCellStyle) workbook.createCellStyle());

if (styleObject.getBgColor() != null) {
    java.awt.Color javaBdgColor = java.awt.Color.decode(voceStyle.getBgColor()); // this is #FFF000
    XSSFColor bgColor = new XSSFColor(javaBdgColor, new DefaultIndexedColorMap());
    cellStyle.setFillForegroundColor(bgColor.getIndex());
    cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
}

Row newRow = Rowsheet.createRow(0);
Cell newCell = newRow.createCell(0);
newCell.setCellStyle(cellStyle);

// write file
String pathFileExport = buildPathExportFile("test-export");
FileOutputStream fileOut = new FileOutputStream(pathFileExport);
workbook.write(fileOut);
fileOut.close();

//close workbook
workbook.close();

return Paths.get(pathFileExport);

I think everything is ok in my code but every cell styled like that will result in a black background. black-cells

I have some doubts about "DefaultIndexedColorMap" instance that's during debugging results without fields:

code debugger

At this point, I'm not sure about what to do to solve. Everyone in other posts seems to get things working but I'm still getting dark backgrounds instead of yellow.

Any suggestions? Thanks in advance!

2

2 Answers

10
votes

As the other answer tells, usage of setFillForegroundColor(XSSFColor color) instead of using indexed colors is necessary in XSSFCellStyle when it comes to customized colors. But usage of indexed colors from org.apache.poi.ss.usermodel.IndexedColors is possible in XSSF too. And this will be the most compatible way if using customized colors is not necessary.

But also creating the XSSFColor from java.awt.Color should be avoided. The constructor XSSFColor(java.awt.Color clr, IndexedColorMap map) is marked "TEST ONLY". And java.awt.Color will not be available in some circumstances.

So if the need is "set cell background color from hex code" and the hex code is in a String, then org.apache.commons.codec.binary.Hex can be used to get an byte[] array from that String. Apache commons codec is one of apache poi's dependencies already. Then constructor XSSFColor(byte[] rgb, IndexedColorMap colorMap) can be used. IndexedColorMap has no usage until now. So it can be set null. If IndexedColorMap gets any usage later, then the code has to be adjusted anyway.

Example:

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;

import org.apache.commons.codec.binary.Hex;

class CreateXSSFColor {

 public static void main(String[] args) throws Exception {

  try (Workbook workbook = new XSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {

   String rgbS = "FFF000";
   byte[] rgbB = Hex.decodeHex(rgbS); // get byte array from hex string
   XSSFColor color = new XSSFColor(rgbB, null); //IndexedColorMap has no usage until now. So it can be set null.

   XSSFCellStyle cellStyle = (XSSFCellStyle) workbook.createCellStyle();
   cellStyle.setFillForegroundColor(color);
   cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);

   Sheet sheet = workbook.createSheet(); 
   Row row = sheet.createRow(0);
   Cell cell = row.createCell(0);
   cell.setCellValue("yellow");
   cell.setCellStyle(cellStyle);

   workbook.write(fileout);
  }

 }
}
2
votes

I've noticed that when working with colors in xlsx files (XSSF), using indexed colors doesn't work very well. It appears that there is no index for any colors in an XSSFWorkbook by default, so you can't use an index of color that isn't indexed.

However, you can use the overload of setFillForegroundColor that directly takes an XSSFColor.

cellStyle.setFillForegroundColor(bgColor);

When I use this overload, then I get the yellow color as a background that you're expecting.

Generally when working with colors in XSSF you should use the XSSFColor itself and not its index. This goes for other things like the other patterned color ("background"), border colors, and font colors.