I have an iterator and the values are like this:
index value
0 A
1 B
2 null
3 null
4 E
5 F
and here is my code:
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
cell = cellIterator.next();
System.out.println(cell.getStringCellValue());
}
and the output is
A
B
E
F
Why does it skip indexes when the value is null? How can I loop over all indexes although the value is null? I want the output to be like:
A
B
null
null
E
F
The null
values aren't showing up because in Excel, the Cell
s don't exist in cells 2 and 3. There are only 4 cells total in that row.
If you want to cover the entire row, even where there aren't cells, you must change from using an iterator to a traditional for loop.
for (int i = 0; i < row.getLastCellNum(); i++)
{
cell = row.getCell(i);
if (cell == null)
{
System.out.println("null");
}
else
{
System.out.println(cell.getStringCellValue());
}
}
The getLastCellNum
method "Gets the index of the last cell contained in this row PLUS ONE .", so the for
loop condition is correct as written.
I had code using the MissingCellPolicy
CREATE_NULL_AS_BLANK
, which will turn a nonexistent Cell
into a blank Cell
, which results in ""
, so we must test whether the cell is present with a null check.
A new, blank cell is created for missing cells. Blank cells are returned as normal
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.