iqueqiorio
iqueqiorio

Reputation: 1187

Apache POI parse and handle empty cells

I am using a java servlet to parse an uploaded excel file,

I get the file as and input stream and make the workbook, and then I want to loop though all the cells including the empty ones, right now this is what I am doing, this handles empty cells in the middle of the data, but if there is a row that is completely empty and then a row with data after it fails

for (int i = 0; i < mySheet.getPhysicalNumberOfRows(); i++) {
    Row row = mySheet.getRow(i);
    for (int j = 0; j < row.getPhysicalNumberOfCells(); j++) {
          Cell cell = row.getCell(j);
    }
}

So it these are my rows

item item item
               //this row is empty
item item item

It is failing how can I handle this?

Thanks for the help

Upvotes: 1

Views: 749

Answers (1)

tech2504
tech2504

Reputation: 977

If row fectching and get back null, then there is no data stored in the file for that row - it's completely blank.

for (int i = 0; i < mySheet.getPhysicalNumberOfRows(); i++) {
    Row row = mySheet.getRow(i);
    for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
           Cell cell = row.getCell(c);
        if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK)
        .....
    }
}

and check poi example POI Example

Upvotes: 2

Related Questions