1

I read all questions on so but no answer was working. If i change the cell color in excel i still get 0 and 64. I am using excel 2007 and poi 3.11. Thanks for help.

try{ File file = new File("C:\\Test\\poi.xlsx");
    FileInputStream fis = new FileInputStream(file);
    XSSFWorkbook wb = new XSSFWorkbook(fis);
    XSSFSheet sh = wb.getSheet("Tabelle1");
    XSSFCellStyle cs = sh.getRow(0).getCell(0).getCellStyle();   
    System.out.println("Color: "+cs.getFillForegroundColor()); // 0
    System.out.println("Color: "+cs.getFillBackgroundColor()); // 64
    }catch(Exception e){e.printStackTrace();}
Zion
  • 723
  • 3
  • 7
  • 24
  • Possible duplicate: http://stackoverflow.com/questions/1499739/how-do-i-get-the-java-apache-poi-hssf-background-color-for-a-given-cell – Ascalonian Dec 23 '14 at 13:46
  • Another possible duplicate: http://stackoverflow.com/questions/19491231/how-to-get-cells-background-color-using-apache-poi – Ascalonian Dec 23 '14 at 13:46
  • The Problem is if i change the color in my cell i still get the same vaules 0 and 64 so the other posts dont answer my question – Zion Dec 23 '14 at 13:52
  • Details like that help in the post haha. Please add that as your code doesn't suggest that either. But from the comments on the other links I posted, apparently 64 is the `Automatic` color – Ascalonian Dec 23 '14 at 14:01
  • Have you tried: `cs.getFillBackgroundColorColor().getRgb()` ? Notice the word `Color` is used twice – Ascalonian Dec 23 '14 at 14:02
  • I tried `cs.getFillBackgroundColorColor().getRgb()` returns null – Zion Dec 23 '14 at 14:07
  • try it with `getFillForegroundColorColor()` as well? With POI, you need to set use `setForegroundColor` to set the background color haha. That's what I meant to type before. – Ascalonian Dec 23 '14 at 14:10
  • I have an excel file created witch Microsoft ecxel 2007 i want to get the color of cell why should i call `setForegroundColor()` ? – Zion Dec 23 '14 at 14:17
  • just try: `cs.getFillForegroundColorColor().getRgb()` – Ascalonian Dec 23 '14 at 14:19
  • I tried it returns a byte array with [-1,-1,-1] – Zion Dec 23 '14 at 14:21
  • That's a byte[] for RGB. You can try: `getARGBHex()` – Ascalonian Dec 23 '14 at 14:27

1 Answers1

1
cs.getFillForegroundColorColor().getARGBHex()

This will return the ARGB in hex

UPDATE
To check the color, you can do:

Color color = cs.getFillForegroundColorColor();

if (color.GREY_25_PERCENT || color.GREY_40_PERCENT || color.GREY_50_PERCENT || color.GREY_80_PERCENT)
    // is grey
}

OR if it's only 2 colors used:

Color color = cs.getFillForegroundColorColor();

if (color.WHITE) {
  // is white
}else {
  // is grey
}
Ascalonian
  • 14,409
  • 18
  • 71
  • 103