Reputation: 77
I was working on excel sheet upload with the help of apache POI framework in java. I inserted an empty column in between filled columns, in some files cell of that particular column is considered as null and in some file the empty cells in column were considered as empty string? Why is this happening? I inserted an empty column in excel using google sheets.
Also, to understand the question, consider the use of Row.CREATE_NULL_AS_BLANK
missing policy we use in row.getCell()
method. When does excel sheet have null value for a cell where we can use this missing policy?
Upvotes: 1
Views: 1357
Reputation: 61915
In a Excel
file in storage for a sheet only rows and cells are stored if they have values or settings. Other rows and cells are not stored. Otherwise each sheet always would must have all possible rows and cells stored even when those rows and cells are not used. That would be a waste of file memory.
Rows have values if they contain cells having values or settings. Rows have settings if they have a row style or a special row height different from default.
Cells have values if they have cell value set. Cells have settings if they have cell styles.
It might also be that cells are stored even if they are totally blank and don't have style. That depends on the spreadsheet software used. Mainly this happens when cells were present and then deleted or when cells were copy/pasted.
The Sheet.getRow
and Row.getCell
methods of apache poi
get not stored rows or cells as null
. In other words, only stored rows and cells are got as not null
. So you always need check null
after Sheet.getRow
and Row.getCell
:
...
Row row = sheet.getRow(3); if (row == null) row = sheet.createRow(3);
Cell cell = row.getCell(3); if (cell == null) cell = row.createCell(3);
...
After that you can be sure having Row row
and Cell cell
not null
.
For Cell
you also can use Row.getCell(int cellnum, Row.MissingCellPolicy policy)
. See (Row.MissingCellPolicy).
There also is CellUtil which provides methods CellUtil.getRow(int rowIndex, Sheet sheet)
and CellUtil.getCell(Row row, int columnIndex)
. Both are creating the row or cell if they cannot be got.
If you only need traversing filled rows and cells, then you can use the iterators as described in Iterate over rows and cells. The iterators never have null
rows or cells. But then you possibly miss totally blank rows or skip blank cells in rows. To avoid that read "Iterate over cells, with control of missing / blank cells" right below.
Upvotes: 3