CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Aug 2008
    Posts
    111

    Question Java Excel automation cell background color repeating setFillForeground

    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:
    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:

    Stackoverflow
    coderanch
    Last edited by StealthRT; October 16th, 2019 at 12:16 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured