How to get cell background color using apache poi?

How do we get background colorfor XSSFCell. I tried to use XSSFCellStylebut 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 cannot get the background color representation in the type Short.

+4
source share
4 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 RGB codes, but not exact ones. But its more or less the same color returned compared to the actual color code in the custom XLS color set.

+2
source

Try the following:

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

Please note that it is Colorused twice.

+1

scala, . .

, , :

val wb = new XSSFWorkbook(path)
for (id <- 0.until(sheetTot)) {
    val sh = wb.getSheetAt(id)    
    print(sh.rowIterator().next().cellIterator().next().getCellStyle().getFillBackgroundColor())
}

64

0

Scala , , . java.awt.Color rgb ( , , , , , Excel). , Scala . , , , getFillBackgroundColorColor()

val rgb: Array[Byte] = cell.getCellStyle.getFillForegroundColorColor.getRgb
def toInt(b: Byte): Int = {
  if (b<0) 256+b else b
}
val rgbInts = rgb.map(toInt)
val color = new Color(rgbInts(0),rgbInts(1),rgbInts(2))
0

All Articles