Frage

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))
            {
War es hilfreich?

Lösung 2

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.

Andere Tipps

CellIterator does not support iterating over null cells. It will automatically skip such cells. To change this behavior, you need to manually iterate over cells (using 0 based index) of a particluar row.

An example of same is described here.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top