SergioLeone
SergioLeone

Reputation: 745

Apache POI. Using row.getLastCellNum() to retrieve number of non blank cells

I'm trying to get non-blank number of cells in a row.

| 1  | 2  | 3 |  |  |  |
| 55 | 33 |   |  |  |  |

with (int) row.getLastCellNum();

But for both rows I'm getting an int that is bigger than the number of cells that actually have values in my sheet. What could be the reason for this? Can cell formatting and/or styling be somehow responsible?

I've also noticed that if I delete values from excel sheet (manually, not from code), poi still considers them as non-blank(null?) and includes them in total count.

UPD: after a bit more research I've found that if I add value to a cell and then delete it, then POI still considers it non-empty. Is there any way to properly clear cell contents in excel sheet without deleting entire row/column?

Upvotes: 1

Views: 2292

Answers (1)

SergioLeone
SergioLeone

Reputation: 745

For now I've solved it like this:

Iterator<Cell> cells = row.cellIterator();
                while (cells.hasNext()) {
                    if (formatCellToString(cells.next()).equals("")) {
                        lastCellNumber = lastCellNumber - 1;
                    }
                }

Would be nice if anyone has any better solution.

Upvotes: 0

Related Questions