getFillForegroundColor,Apache POI

所以我使用Apache POI库来读取一个Excel文件(.xlsx),我正在做的事情很简单:我想遍历所有的单元格,但只读了红色\黄色\绿色\蓝色的文本细胞,并跳过所有其他人。

假设:

  • 加载的文件不是空的,包含着色单元格,
  • 我导入了所有需要的库。

遵循指南( http://poi.apache.org/spreadsheet/quick-guide.html#Iterator ),我结束了编码:

... ... // why 15? 1400? no idea.. the guide doesn't explain... also isn't it dangerous?? int rowStart = Math.min(15, sheet.getFirstRowNum()); int rowEnd = Math.max(1400, sheet.getLastRowNum()); // iterate through ROWS: for (int rowNum = rowStart; rowNum < rowEnd; rowNum++) { // returns 'rowNum' row of current sheet: Row r = sheet.getRow(rowNum); if(r == null) { System.out.println("empty row skipped"); continue; } // iterate through CELLS: for (Cell cell : r) { if(cell == null) { System.out.println("empty cell"); continue; } XSSFColor colore = (XSSFColor) cell.getCellStyle().getFillForegroundColorColor(); if (colore == null) { System.out.println("something wrong?"); continue; } // checking colors like this: if(colore.getARGBHex().substring(2, 5).equals("FFFF")){ // working on cell continue; } if(colore.getARGBHex().substring(4, 5).equals("FF")){ // working on cell continue; } if(colore.getARGBHex().substring(6, 7).equals("FF")){ // working on cell continue; } if(colore.getARGBHex().substring(2, 3).equals("FF")){ // working on cell continue; } } 

当我执行代码时,它最终打印“出错了? 千次,好像colore在所有的细胞中都是空的。 所以问题是:

  1. 为什么它alwaisfindcolore null? (我也试过.getFillBackgroundColorColor())
  2. 有没有更聪明的方法来检查颜色,而不是使用那些可怕的if语句?

提前致谢!