Sandeep
Sandeep

Reputation: 81

how do i check that my cells in a an excel file are not null for a specific 1st cell value, through java

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

Answers (1)

Gagravarr
Gagravarr

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

Related Questions