如何确定空行?(How to determine empty row?)

2019-06-27 15:40发布

如何确定在使用Apache POI的.xls文件空行?

Answer 1:

我用下面的方法在我的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;
}


Answer 2:

The row iterator returns only rows that contain data, however if they are completely empty then iterating by row index, getRow(index) returns null

Solution:

Up to POI version 3.14 (thanks to 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;
}

From POI version 3.15 to 4.2 (int getCellType() is deprecated):

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

From POI version 4 (CellTypeEnum getCellTypeEnum() will return the Enum not 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;
}


Answer 3:

你必须通过行中的所有细胞进行迭代,并检查它们都是空的。 我不知道任何其他解决方案...

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

该代码是从这里



Answer 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;
   }


Answer 5:

假设你要检查,如果行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;
       }
    }
 }


Answer 6:

尝试使用IF(iterator.hasNext)

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


Answer 7:

如果您正在使用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 string当细胞或者是nullBLANK

供您参考import语句:

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;

希望这可以帮助!



Answer 8:

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


Answer 9:

得到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/



文章来源: How to determine empty row?