How to determine empty row?

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

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

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

    I'm using the following method in my POI project and it's working well. It is a variation of zeller's solution.

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

    Yes, but if in some row we will have in some cell = " " and empty values in another cells. This method will be work better:

      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;
       }
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-12-23 10:28

    get the instance of CellReference and use formatAsString() on the instance. compare it with an empty String

    `

    if("".equals(cellRef.formatAsString())){
         System.out.println("this is an empty cell");
       }else{
          System.out.println("Cell value : "+cellRef.formatAsString());
       }
    

    ` Reference : http://www.javabeat.net/2007/10/apache-poi-reading-excel-sheet-using-java/

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

    You have to iterate through all cells in the row and check if they are all empty. I don't know any other solution...

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

    The code is from here

    0 讨论(0)
提交回复
热议问题