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?
Asked
Active
Viewed 184 times
0
-
have a look here it http://stackoverflow.com/questions/18072847/null-pointer-exception-apache-poi it is related – Abhishek Singh Nov 28 '13 at 10:44
1 Answers
1
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");
}
}
}

Gagravarr
- 47,320
- 10
- 111
- 156