Sometimes using row.getLastCellNum()
gives you a higher value than what is actually filled in the file.
I used the method below to get the last column index that contains an actual value.
private int getLastFilledCellPosition(Row row) {
int columnIndex = -1;
for (int i = row.getLastCellNum() - 1; i >= 0; i--) {
Cell cell = row.getCell(i);
if (cell == null || CellType.BLANK.equals(cell.getCellType()) || StringUtils.isBlank(cell.getStringCellValue())) {
continue;
} else {
columnIndex = cell.getColumnIndex();
break;
}
}
return columnIndex;
}