Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get cell's background color using apache poi?

How do we get background color of a XSSFCell. I tried using XSSFCellStyle but no luck.

FileInputStream fis = new FileInputStream(fileName);
XSSFWorkbook book = new XSSFWorkbook(fis);
XSSFSheet sheet = book.getSheetAt(0);
XSSFRow row = sheet.getRow(0);

System.out.println(row.getCell(0).getCellStyle().getFillForegroundColor());

Using these steps I am not able to get background color representation in Short type.

like image 596
AAA Avatar asked Oct 21 '13 09:10

AAA


2 Answers

Checkout this URL:

https://issues.apache.org/bugzilla/show_bug.cgi?id=45492

Cell cell = row.getCell(1);
            CellStyle cellStyle = cell.getCellStyle();          
            System.out.println("color = " + getColorPattern(cellStyle.getFillForegroundColor()));




private short[] getColorPattern(short colorIdx){        
    short[] triplet = null;
    HSSFColor color = palette.getColor(colorIdx);
    triplet = color.getTriplet();       
    System.out.println("color : " + triplet[0] +"," + triplet[1] + "," + triplet[2]);
    return triplet;
}

This returns the RGB codes but not exact ones. But its more or less the same color returned when compared with the actual color code in the XLS custom color picker.

like image 70
Kartik Narayana Maringanti Avatar answered Oct 04 '22 02:10

Kartik Narayana Maringanti


Try this:

row.getCell(0).getCellStyle().getFillForegroundColorColor().getARGBHex()

Notice that Color is used twice

like image 44
Ascalonian Avatar answered Oct 04 '22 03:10

Ascalonian