I am reading a particular excel template in Java using Apache POI. There are some unwanted fields at the bottom (like a small guide) which I need to skip. So I want to stop the iteration whenever POI finds that a row is completely blank and hence the fields at the bottom will be skipped automatically.
I found many ways to handle a blank CELL but my requirement is for a complete row.
Following code will iterate through the cells of a row. it will keep a count for each cell it encounters(cellCount). another couont(nullCount), is incremented only if the cell is blank. if both these counts are equal, row is empty.
boolean isEmptyRow = false;
int cellCount = 0;
int nullCount = 0;
Iterator<Cell> cellIterator = row.iterator();
while (cellIterator.hasNext()) {
cellCount++;
Cell cell = (Cell) cellIterator.next();
if ((cell == null)
|| ((cell != null) && (cell.getCellType() == Cell.CELL_TYPE_BLANK))) {
nullCount++;
}
}
if ((cellCount != 0) && (nullCount != 0)
&& (cellCount == nullCount)) { //If nullCount & cellCouont are same, Row is empty
isEmptyRow = true;
}
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.