2012-08-31 33 views

回答

5

您必須迭代該行中的所有單元格,並檢查它們是否全部爲空。我不知道任何其他解決方案...

short c; 
for (c = lastRow.getFirstCellNum(); c <= lastRow.getLastCellNum(); c++) { 
    cell = lastRow.getCell(c); 
    if (cell != null && lastRow.getCell(c).getCellType() != HSSFCell.CELL_TYPE_BLANK) { 
      nonBlankRowFound = true; 
    } 
} 

的代碼是從here

2

假設你要檢查,如果行n是空的,記住了Apache POI行是基於而不是一個從零開始,你想要的東西是這樣的:

Row r = sheet.getRow(n-1); // 2nd row = row 1 
boolean hasData = true; 

if (r == null) { 
    // Row has never been used 
    hasData = false; 
} else { 
    // Check to see if all cells in the row are blank (empty) 
    hasData = false; 
    for (Cell c : r) { 
     if (c.getCellType() != Cell.CELL_TYPE_BLANK) { 
     hasData = true; 
     break; 
     } 
    } 
} 
26

我在我的POI項目中使用以下方法,它是運作良好。這是澤勒解決方案的變體。

public static boolean isRowEmpty(Row row) { 
    for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) { 
     Cell cell = row.getCell(c); 
     if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) 
      return false; 
    } 
    return true; 
} 
+0

我已經使用了相同的一段代碼,但它爲單元格值爲空返回false。如果我檢查是否(cell.getStringCellValue!= null && cell.getNumericValue!= null)它適用於String值,但對於BigDecimal值,它返回IllegalStateException:無法將文本轉換爲數值。你可以建議我如何處理isRowEmpty以返回true爲空行嗎? – Harleen

+0

如果單元格類型是公式,該怎麼辦? –

3

是的,但如果在一些行,我們將有一些細胞 = 「」和空值在另一個細胞。 這種方法會更好地工作:

boolean isEmptyRow(Row row){ 
    boolean isEmptyRow = true; 
     for(int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++){ 
      Cell cell = row.getCell(cellNum); 
      if(cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK && StringUtils.isNotBlank(cell.toString())){ 
      isEmptyRow = false; 
      }  
     } 
    return isEmptyRow; 
    } 
12

行迭代器只返回包含數據的行,但如果它們是完全空的,則迭代通過行索引,getRow(index)回報null

解決方案:

到POI版本3.14(感謝Sergii Lisnychyi):

private boolean checkIfRowIsEmpty(Row row) { 
    if (row == null) { 
     return true; 
    } 
    if (row.getLastCellNum() <= 0) { 
     return true; 
    } 
    for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) { 
     Cell cell = row.getCell(cellNum); 
     if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK && StringUtils.isNotBlank(cell.toString())) { 
      return false; 
     } 
    } 
    return true; 
} 

從POI版本3.15至4.2(int getCellType()已經過時):

private boolean checkIfRowIsEmpty(Row row) { 
    if (row == null) { 
     return true; 
    } 
    if (row.getLastCellNum() <= 0) { 
     return true; 
    } 
    for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) { 
     Cell cell = row.getCell(cellNum); 
     if (cell != null && cell.getCellTypeEnum() != CellType.BLANK && StringUtils.isNotBlank(cell.toString())) { 
      return false; 
     } 
    } 
    return true; 
} 

從POI版本4(CellTypeEnum getCellType()將返回枚舉不是int):

private boolean checkIfRowIsEmpty(Row row) { 
    if (row == null) { 
     return true; 
    } 
    if (row.getLastCellNum() <= 0) { 
     return true; 
    } 
    for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) { 
     Cell cell = row.getCell(cellNum); 
     if (cell != null && cell.getCellType() != CellType.BLANK && StringUtils.isNotBlank(cell.toString())) { 
      return false; 
     } 
    } 
    return true; 
} 
0

嘗試使用IF(iterator.hasNext)

Row nextRow = null; 
Cell nextCell = null; 
Iterator<Row> iterator = firstSheet.rowIterator(); 
if(iterator.hasNext) { 
    return true; 
} 
else { 
    return false; 
}