Reputation: 803
I have a excel sheet with 5 records, I have to add only first 4 cell in a row to the List of object and ignore rest of the cells. If the first 4 cell are empty or blank then I need to ignore that row and did not add into object.The catch is there are some records where in row first 4 cell are empty but after that cell have some value. I don't want to include those rows in the object. I have tried to put a piece of code:
public static boolean isRowEmpty(Row row){
for(int c = row.getFirstCellNum(); c<row.getLastCellNum();c++){
Cell cell = row.getCell(c);
if(cell!=null && cell.getCellType()!=Cell.CELL_TYPE_BLANK){
return false;
}
}
return true;
}
if(isRowEmpty(row)==false){
compList.add(compItem);
}
This piece of code works only when row is empty and isRowEmpty() method returns true. So far good, it didn't add value to the list. If the first 4 cell are blank and rest cell have value in a row, isRowEmpty() returns false and null value is set to list of object. Can somebody suggest me a solution to fix it?
Upvotes: 0
Views: 9208
Reputation: 1626
I'm still not entirely sure what the question is. My best guess is that you want to inspect the first 4 columns of each row. If the 4 cells are blank, the row is ignored.
The issue is when cells exist in the columns that come after the forth column, they are added to the list (instead of ignoring the row).
If that is the case, then this should help:
public static boolean isRowEmpty(Row row){
for(int c = row.getFirstCellNum(); c < 4 && c < row.getLastCellNum(); c++){
Cell cell = row.getCell(c);
if(cell!=null && cell.getCellType()!=Cell.CELL_TYPE_BLANK){
return false;
}
}
return true;
}
if(isRowEmpty(row)==false){
compList.add(compItem);
}
Upvotes: 1
Reputation: 1981
You are checking all the elements in the row. If you limit the number of elements checked to first 4 in the row, you'd achieve the expected results.
public static boolean isRowEmpty(Row row){
int firstCol = row.getFirstCellNum();
for(int cnt = 0; cnt<4 ; cnt++){
Cell cell = row.getCell(firstCol+cnt);
if(cell!=null && cell.getCellType()!=Cell.CELL_TYPE_BLANK){
return false;
}
}
return true;
}
if(isRowEmpty(row)==false){
compList.add(compItem);
}
Upvotes: 2