Raviteja
Raviteja

Reputation: 13

getFillForegroundColor() is returning 0 for a coloured cell

While reading color of a XSSFCell, cellStyle.getFillForegroundColor() is returning 0 even though there is some user defined color in the cell. But i am able to get the non-zero value for few well known colors.

 for ( int rowNo=0; rowNo<=sheet.getLastRowNum(); rowNo++){ 
     Row row = sheet.getRow(rowNo); 
     for(int cellNo=0; cellNo<row.getLastCellNum(); cellNo++) { 
       Cell cell = row.getCell(cellNo, Row.CREATE_NULL_AS_BLANK); 
       CellStyle cellStyle = cell.getCellStyle();
       System.out.println("ForegroundColor:"+cellStyle.getFillForegroundColor());
       System.out.println("BackgroundColor:"+cellStyle.getFillBackgroundColor()); 
      } 
    }

Please suggest on how to read the custom color value from XLSX file.

Upvotes: 1

Views: 917

Answers (1)

centic
centic

Reputation: 15872

Excel has two different notions of color, an indexed one and a custom one. Indexed colors are predefined colors where the color-value is defined by Microsoft. Custom colors store the usual RGB-value triplet.

XSSFCellStyle has different functions depending if you want to read indexed or custom color, e.g. getFillForegroundColor() returns the indexed color, getFillForegroundColorColor() returns a custom color.

These methods will not try to "convert", so if you have a custom color, the indexed-method will return 0 and if you have an indexed color, requesting the custom color will return null.

See also the related JavaDoc.

Upvotes: 1

Related Questions