title: Apache POI 4.0: XSSFColor from java.awt.Color
body: <p><code>org.apache.poi 4.0</code> removed the <code>XSSFColor</code> constructor that just uses <code>java.awt.Color</code>. In <code>org.apache.poi 3.7</code> it was very easy to create the object by just writing</p>
  
  <pre><code>Color inputColor = Color.RED;
  XSSFColor test = new XSSFColor(inputColor);
  </code></pre>
  
  <p>However, this constructor no longer works in 4.0. The documentation at <a href="https://poi.apache.org/apidocs/dev/org/apache/poi/xssf/usermodel/XSSFColor.html" rel="nofollow noreferrer">https://poi.apache.org/apidocs/dev/org/apache/poi/xssf/usermodel/XSSFColor.html</a> shows several other constructors, but ideally i want to change as few lines as possible. </p>
  
  <p>So, my question is, what is the best way to create an <code>XSSFColor</code> from a <code>java.awt.Color</code> now (in apache poi 4.0)?</p>
  
  <hr>
  
  <p>As requested in the comments, here is my test code using the suggestion <code>style.setFillForegroundColor(new XSSFColor(java.awt.Color.RED, null));</code>
  Opening this with LibreOffice 6.1 yields an Error (Attempt to repair, which then fails). Commented out the POI 3.7 version which works normally.</p>
  
  <pre><code>@Test
  public void testPOI40() throws FileNotFoundException, IOException {
      Workbook workbook = new XSSFWorkbook();
      XSSFSheet fSheet = (XSSFSheet) workbook.createSheet("new Sheet");
      XSSFRow hRow = fSheet.createRow((short) 0);
      //header
      String[] astrHeaders = new String[]{"Header1", "Header2", "Header3", "Header4"};
      for (int col = 0; col &lt; astrHeaders.length; col++) {
          XSSFCell cell = hRow.createCell((short) col);
          XSSFCellStyle tempHeaderStyle = (XSSFCellStyle) workbook.createCellStyle();
          tempHeaderStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
          tempHeaderStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
          cell.setCellValue(astrHeaders[col]);
          cell.setCellStyle(tempHeaderStyle);
      }        
      //body
      Double[] astrContent = new Double[]{1.3, 0.3, 0.87, 1.0};     
      Color[] colors = new Color[] {Color.RED,Color.BLUE,Color.WHITE,Color.GREEN};        
      XSSFRow fRow = fSheet.createRow((short) 1);
      for (int iCol = 0; iCol &lt; 4; iCol++) {
          XSSFCell cell = fRow.createCell((short) iCol);
          XSSFCellStyle tempBodyStyle = (XSSFCellStyle) workbook.createCellStyle();
          cell.setCellValue(astrContent[iCol]);
          //working with POI 3.17
          //tempBodyStyle.setFillForegroundColor(new XSSFColor(colors[iCol]));
          tempBodyStyle.setFillForegroundColor(new XSSFColor(colors[iCol],null));
          tempBodyStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
          cell.setCellStyle(tempBodyStyle);
      }        
      FileOutputStream fileOut = new FileOutputStream(new File("testfile.xlsx"));
      BufferedOutputStream bos = new BufferedOutputStream(fileOut);
      workbook.write(bos);
      fileOut.close();
  }
  </code></pre>
  
  <p><strong>Solution:</strong> <br>
  Replaced <code>fileout.close();</code> with <code>bos.close();</code> and it works. So <code>tempBodyStyle.setFillForegroundColor(new XSSFColor(Color.RED,null));</code> as suggested in the comments by Alex Richter is a good solution &amp; will accept this as answer.</p>