Missing cell policy of Apache POI Java
Did you read the Apache POI Excel Busy Developer's Guide?
In some cases, when iterating, you need full control over how missing or blank rows and cells are treated, and you need to ensure you visit every cell and not just those defined in the file. (The CellIterator will only return the cells defined in the file, which is largely those with values or stylings, but it depends on Excel).
In cases such as these, you should fetch the first and last column information for a row, then call getCell(int, MissingCellPolicy) to fetch the cell. Use a MissingCellPolicy to control how blank or null cells are handled.
If you're iterating over columns in a row, some cells that are blank may not even exist, which may causing unsuspecting code to throw a NullPointerException
. A MissingCellPolicy
, when passed to getCell
, guides and simplifies code that tells Apache POI how to handle these kinds of cells.
- CREATE_NULL_AS_BLANK - If the
Cell
returned doesn't exist, instead of returningnull
, create a newCell
with a cell type of "blank". This can help avoidNullPointerException
s conveniently. - RETURN_BLANK_AS_NULL - Even if the cell exists but has a cell type of "blank", return
null
. This can allow you ignore blank cells that do exist easily. - RETURN_NULL_AND_BLANK - Don't modify the existing structure; return
null
for cells that don't really exist and return the blankCell
if it exists but its cell type is blank. This is the behavior of thegetCell
overload that doesn't take aMissingCellPolicy
.