title: Changing cell color using apache poi
body: <p>I'm using Apache POI to read data in a spreadsheet of part numbers.  I look up the part number in our database, if we have a CAD drawing of the part I color the part number cell green, if we don't I color it red.  After the processing is done the spreadsheet is saved.  The problem I'm having is that every cell in that column comes out green.  I've stepped through the code, the logic to look up the part number is working fine and the logic to determine what color the cell should be and setting the color and fill appears to work as well.  Any ideas what I'm doing wrong here?</p>
  
  <p>Thanks.</p>
  
  <pre><code>//Check the parts
  for(int r=1;r&lt;sheet.getPhysicalNumberOfRows();r++) {
      String partNumber = null;
      switch(cell.getCellType()) {
          case HSSFCell.CELL_TYPE_NUMERIC:
              long pNum = (long) cell.getNumericCellValue();
              partNumber = String.valueOf(pNum);
              break;
          case HSSFCell.CELL_TYPE_STRING:
              partNumber = cell.getStringCellValue();
              break;
          default:
              logger.info("Part Number at row " + r + " on sheet " + partList.getSheetName(s) + "is of an unsupported type");
      }
  
      try {
          List&lt;String&gt; oldMaterialNumbers = getOldMaterialNumbers(partNumber);
  
          boolean gotDrawing = checkPartNumber(oldMaterialNumbers, partNumber);
          //If there's a drawing then color the row green, if not red.
          short bgColorIndex = gotDrawing
                                  ?HSSFColor.LIGHT_GREEN.index //42
                                  :HSSFColor.RED.index; //10
  
          HSSFCell curCell = row.getCell(partNumberColumn);
          HSSFCellStyle curStyle = curCell.getCellStyle();
  
          curStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
          curStyle.setFillForegroundColor(bgColorIndex);
  
          curCell.setCellStyle(curStyle);
  
      }catch(Exception e) {
          throw e;
      }
  }
  </code></pre>