How to determine empty row?

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

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

9条回答
  •  旧时难觅i
    2020-12-23 10:25

    If you are using apache-poi [4+]:

    Then the below method works for you. As the other methods suggested did not work for me, I had to do it this way.

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

    The method dataFormatter.formatCellValue(cell) would return "", an empty / ZERO length string when the cell is either null or BLANK.

    The import statements for your reference:

    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;
    

    Hope this helps!

提交回复
热议问题