38

如何使用 Apache POI 确定 .xls 文档中的空行?

4

10 回答 10

44

我在我的 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;
}
于 2013-05-06T16:23:18.510 回答
32

行迭代器仅返回包含数据的行,但是如果它们完全为空,则按行索引进行迭代,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;
}

从 POI 版本 5.1(CellTypeEnum getCellTypeEnum() 重命名为 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.getCellType() != CellType.BLANK && StringUtils.isNotBlank(cell.toString())) {
            return false;
        }
    }
    return true;
}
于 2015-02-11T10:19:52.367 回答
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;
     }
 }

代码来自这里

于 2012-08-31T14:12:16.623 回答
4

是的,但是如果在某些中,我们将在某个单元格= " "中包含另一个单元格中的空值。这种方法会更好:

  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;
   }
于 2013-11-15T13:55:17.293 回答
3

假设您想检查行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;
       }
    }
 }
于 2012-08-31T15:01:51.957 回答
3

如果您使用的是 apache-poi [4+]:

那么下面的方法对你有用。由于建议的其他方法对我不起作用,我不得不这样做。

public static boolean isRowEmpty(Row row) {
    boolean isEmpty = true;
    DataFormatter dataFormatter = new DataFormatter();
    if(row != null) {
        for(Cell cell: row) {
            if(dataFormatter.formatCellValue(cell).trim().length() > 0) {
                isEmpty = false;
                break;
            }
        }
    }
    return isEmpty;
}

当单元格为或时,该方法dataFormatter.formatCellValue(cell)将返回。""empty / ZERO length stringnullBLANK

供您参考的导入语句:

import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;

希望这可以帮助!

于 2019-01-19T06:12:06.217 回答
3
boolean isEmptyRow(Row row) {
    boolean isEmpty=true;
    String data="";
    for(Cell cell:row) {
        data=data.concat(cell.getStringCellValue());
    }
    if(!data.trim().isEmpty()) {
        isEmpty=false;
    }
    return isEmpty;
}
于 2019-05-17T10:45:14.240 回答
2

尝试使用 if(iterator.hasNext)

Row nextRow = null;
Cell nextCell = null;
Iterator<Row> iterator = firstSheet.rowIterator();
if(iterator.hasNext) {
    return true;
}
else {
    return false;
}
于 2017-03-01T11:33:24.833 回答
0

获取 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/

于 2012-08-31T14:24:37.193 回答
0

Row == null在我的情况下是有效的。

int total_row = mySheet.getLastRowNum();
int current_row = 0;
HSSFRow hssf_Row;

while (current_row <= total_row) {
    hssf_Row = mySheet.getRow(current_row);
    if (hssf_Row == null) Log.d("empty row","row=" + String.valueOf(current_row));
    current_row++;
}
于 2021-05-18T11:41:09.000 回答