简体   繁体   中英

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?

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");
      }
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM