title: APACHE POI 4.1 : Set cell background color from hex code
body: <p>I've tried different solutions posted on stack overflow to apply a background color to an Apache POI generated cell, but nothing worked.</p>
  
  <p>I'm doing something like:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>I think everything is ok in my code but every cell styled like that will result in a black background.
  <a href="https://i.stack.imgur.com/a33Ut.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/a33Ut.jpg" alt="black-cells"></a></p>
  
  <p>I have some doubts about "DefaultIndexedColorMap" instance that's during debugging results without fields:</p>
  
  <p><a href="https://i.stack.imgur.com/mi4wq.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mi4wq.jpg" alt="code debugger"></a></p>
  
  <p>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.</p>
  
  <p>Any suggestions?
  Thanks in advance!</p>