javaexcelautomationapache-poipoi-hssf

Java Excel automation cell background color repeating setFillForegroundColor


Hey all I have the code below that changes the background of a cell to either RED or GREEN. It seems that when I comment out the GREEN else code that my excel sheet has all red for each cell box in row 1. Likewise, if I do the opposite and comment out RED and un-comment GREEN then all the cells in row 1 are green.

I'm not understanding what in the code below is making it color all the cells the same color even though the first 2 cells should be RED while all the others should be GREEN. I already checked my logic and its going to the first IF 2 times and then the rest is going to the else so that's correct.

My code:

static CellStyle headerCellStyle    = workbook.createCellStyle();

for (int i = 0; i < arr.length; i++) {
     Row row             = sheet.createRow(rowNum1++);
     HSSFWorkbook hwb    = new HSSFWorkbook();
     HSSFPalette palette = hwb.getCustomPalette();
     Cell cell           = row.createCell(colNum);

     headerCellStyle.setWrapText(true);
     headerCellStyle.setAlignment(HorizontalAlignment.LEFT);
     headerCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
     headerCellStyle.setAlignment(HorizontalAlignment.JUSTIFY);
     headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

     if (arr[i].contains("*")) {
         //RED
         headerCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
         cell.setCellValue(arr[i].replace(" " + (i + 1) + ".xml*", ""));
     } else {
         //GREEN
         headerCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
         cell.setCellValue(arr[i].replace(" " + (i + 1) + ".xml", ""));
     }

     row.getCell(0).setCellStyle(headerCellStyle);
     row.setHeightInPoints(20);
}

I'm sure I am just looking over something quite obvious but at this time I'm not able to find what that may be.

Any help would be great!

Note: Also posted to the following forums:

coderanch.com

codeguru


Solution

  • The cell fills are stored in the cell styles and those are stored on workbook level. So never do creating the cell styles in same loop which sets cell values into the sheet.

    If two different cell fills (one having red and one having green solid pattern) are needed, then also two cell styles are needed. Those needs to be created on workbook level first and then be set as cell style within the loop which sets the cell values.

    Your code is not complete, so I can only guess, what exactly you are trying to achieve.

    I hope the following minimal but complete example will help you:

    import java.io.FileOutputStream;
    
    import org.apache.poi.ss.usermodel.*;
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    public class CreateExcelCellStyleRedAndGreen {
    
     public static void main(String[] args) throws Exception {
      //Workbook workbook = new XSSFWorkbook();
      Workbook workbook = new HSSFWorkbook();
    
      CellStyle headerCellStyleRed = workbook.createCellStyle();
      CellStyle headerCellStyleGreen = workbook.createCellStyle();
    
      headerCellStyleRed.setWrapText(true);
      headerCellStyleRed.setFillPattern(FillPatternType.SOLID_FOREGROUND);
      headerCellStyleRed.setAlignment(HorizontalAlignment.JUSTIFY);
      headerCellStyleRed.setVerticalAlignment(VerticalAlignment.CENTER);
    
      headerCellStyleGreen.cloneStyleFrom(headerCellStyleRed);
    
      headerCellStyleRed.setFillForegroundColor(IndexedColors.RED.getIndex());
      headerCellStyleGreen.setFillForegroundColor(IndexedColors.GREEN.getIndex());
    
      String[] arr = new String[] {
       "A Name of File.xml", 
       "B Name of File.xml*", 
       "C Name of File.xml", 
       "D Name of File.xml", 
       "E Name of File.xml*", 
       "F Name of File.xml"
      };
    
      int rowNum=1;
      int colNum=1;
    
      Sheet sheet = workbook.createSheet();
    
      for (int i = 0; i < arr.length; i++) {
       Row row = sheet.createRow(rowNum++);
       Cell cell = row.createCell(colNum);
       if (arr[i].contains("*")) {
        //RED
        cell.setCellStyle(headerCellStyleRed);
        cell.setCellValue(arr[i].replace(".xml*", ""));
    
       } else {
        //GREEN
        cell.setCellStyle(headerCellStyleGreen);
        cell.setCellValue(arr[i].replace(".xml", ""));
       }
       row.setHeightInPoints(50);
      }
    
      FileOutputStream out = null;
      if (workbook instanceof HSSFWorkbook) {
       out = new FileOutputStream("CreateExcelCellStyleRedAndGreen.xls");
      } else if (workbook instanceof XSSFWorkbook) {
       out = new FileOutputStream("CreateExcelCellStyleRedAndGreen.xlsx");
      }
      workbook.write(out);
      out.close();
      workbook.close();
     }
    }