Reputation: 81
I have an excel sheet. In that sheet i have to check that atleast two of the cells out of Cell No. 2, 3, 4 and 5 should not be null if Cell No. 1 is not null. How do i do that through java POI API?
Upvotes: 0
Views: 213
Reputation: 48326
It's pretty easy to do! Assuming you want to check each row in turn, you'd want something like:
for (Row r : sheet) {
Cell c = r.getCell(0, Row.RETURN_BLANK_AS_NULL);
if (c == null) {
// First cell is empty
} else {
// First cell has data
// Count how many of the next 4 cells are filled
int next4Filled = 0;
for (int i=1; i<=4; i++) {
c = r.getCell(i, Row.RETURN_BLANK_AS_NULL);
if (c != null) next4Filled++;
}
if (next4Filled < 2) {
throw new IllegalStateException("Too many cells empty in row");
}
}
}
Upvotes: 1