Get Cell color in excel with poi

守給你的承諾、 提交于 2019-12-24 10:49:29

问题


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();}

回答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
}


来源:https://stackoverflow.com/questions/27621527/get-cell-color-in-excel-with-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!