How to determine empty row?

后端 未结 9 1827
情书的邮戳
情书的邮戳 2020-12-23 10:18

How I can determine empty rows in .xls documents using Apache POI?

相关标签:
9条回答
  • 2020-12-23 10:40

    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;
    }
    
    0 讨论(0)
  • 2020-12-23 10:43

    try using if(iterator.hasNext)

    Row nextRow = null;
    Cell nextCell = null;
    Iterator<Row> iterator = firstSheet.rowIterator();
    if(iterator.hasNext) {
        return true;
    }
    else {
        return false;
    }
    
    0 讨论(0)
  • 2020-12-23 10:48

    Assuming you want to check if row n is empty, remembering that rows in Apache POI are zero based not one based, you'd want something like:

     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;
           }
        }
     }
    
    0 讨论(0)
提交回复
热议问题