Search code examples
springexcelapache-poixssfpoi-hssf

Why is my code skipping blank fields?


I'm using apache poi to read from excel files but when it comes to the empty cells it skips the cell. Why is it so? I've placed a if else condition to catch it. Here's my code:

while (cellIterator.hasNext()) {

    cell = (XSSFCell) cellIterator.next();
        if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
        {
            if(DateUtil.isCellDateFormatted(cell))
            {

Solution

  • I came across a good solution to that problem.If you want to get all cells, no matter if they exist or not, then the iterator isn't for you. Instead, you need to manually fetch the appropriate cells, likely with a missing cell policy

    for(Row row : sheet) {
       for(int cn=0; cn<row.getLastCellNum(); cn++) {
           // If the cell is missing from the file, generate a blank one
           // (Works by specifying a MissingCellPolicy)
           Cell cell = row.getCell(cn, Row.CREATE_NULL_AS_BLANK);
           // Print the cell for debugging`enter code here`
           System.out.println("CELL: " + cn + " --> " + cell.toString());
       }
    }
    

    It works. This converts all the missing rows as blanks and than the CELL_TYPE_BLANK will catch it.