如何确定空行?
如何使用Apache POI确定.xls文档中的空行?
我在我的POI项目中使用以下方法,它运行良好。 这是zeller解决方案的变体。
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; }
行迭代器只返回包含数据的行,但如果它们完全为空, getRow(index)
行索引进行迭代, 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 getCellTypeEnum()
将返回Enum而不是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.getCellTypeEnum() != CellType.BLANK && StringUtils.isNotBlank(cell.toString())) { return false; } } return true; }
您必须遍历行中的所有单元格并检查它们是否全部为空。 我不知道任何其他解决方案……
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; } }
代码来自这里
是的,但如果在某一行,我们将在某个单元格中使用 “”“并在另一个单元格中显示空值。 这种方法会更好用:
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; }
假设您要检查行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; } } }
尝试使用if(iterator.hasNext)
Row nextRow = null; Cell nextCell = null; Iterator iterator = firstSheet.rowIterator(); if(iterator.hasNext) { return true; } else { return false; }
获取CellReference的实例并在实例上使用formatAsString()。 将它与空字符串进行比较
`
if("".equals(cellRef.formatAsString())){ System.out.println("this is an empty cell"); }else{ System.out.println("Cell value : "+cellRef.formatAsString()); }
`参考: http : //www.javabeat.net/2007/10/apache-poi-reading-excel-sheet-using-java/