title: Java apache poi: excel cell color
body: <p>I'm trying to change the background of a cell with <a href="https://poi.apache.org/" rel="nofollow noreferrer">apache poi</a>.</p>
  
  <p><strong>I know</strong> there are a lot of answers about this, but I'm using the lastest  version, (3.16) and they are all <strong>deprecated</strong>.</p>
  
  <p>For example all the answers suggest that I use </p>
  
  <pre><code>CellStyle#setFillPattern(CellStyle.SOLID_FOREGROUND);
  </code></pre>
  
  <p>but it's deprecated entirely.</p>
  
  <p>So, following the apache docs, I replaced all the deprecated functions with the new ones and came up with this MCVE:</p>
  
  <pre><code>import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import org.apache.poi.ss.usermodel.Cell;
  import org.apache.poi.ss.usermodel.FillPatternType;
  import org.apache.poi.ss.usermodel.IndexedColors;
  import org.apache.poi.ss.usermodel.Row;
  import org.apache.poi.xssf.usermodel.XSSFCellStyle;
  import org.apache.poi.xssf.usermodel.XSSFSheet;
  import org.apache.poi.xssf.usermodel.XSSFWorkbook;
  
  public class Mcve{
      public static void main(String[] args) {
  
      //Make workbook and first sheet
      XSSFWorkbook workbook = new XSSFWorkbook();
      XSSFSheet sheet = workbook.createSheet("sheet1");
  
      //Make a style
      XSSFCellStyle style = workbook.createCellStyle();
      style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
      style.setFillBackgroundColor(IndexedColors.RED.getIndex());
  
      //Fill first line
      Row row = sheet.createRow(0);
  
      int i = 0;
      while (i &lt; 5) {
  
          Cell cell = row.createCell(i);
          cell.setCellValue("TestCell " + i++);
                      cell.setCellStyle(style);
      }
  
      //Write to file
      File f = new File("Yourfilepathhere/document.xlsx"); //&lt;-- FILL HERE
  
      try (FileOutputStream out = new FileOutputStream(f)) {
          workbook.write(out);
          workbook.close();
      } catch (Exception e) {
          e.printStackTrace();
  
      }
  }
  }
  </code></pre>
  
  <p>I suggest you paste it in an empty Maven project in your IDE of choice and please add these dependencies to your <code>pom.xml</code>:</p>
  
  <p><a href="https://pastebin.com/CXdViuW5" rel="nofollow noreferrer">https://pastebin.com/CXdViuW5</a></p>
  
  <p>Now, on the lastest version of <em>Excel</em>, this either prints all-black cells or normal white-background cells, depending on the color.
  I have tried several colors and styles, doesn't seem to be working. The text is always there, but the background just won't apply.</p>
  
  <p>What am I doing wrong here guys?</p>