什么应该是破解条件退出for循环,而parsingexcel表?

  • 我有一个Excel表。 我parsing它使用Java POI。

  • 我需要在for循环的帮助下parsing6-15行。

  • 在第16行,第一个单元格是空的,第三个和第四个单元格中有值。 那么当计数器到达第16行时,什么情况下应该跳脱出来呢?

注意:对于不同的Excel工作表,迭代值将会改变。 因此我需要一个通用的循环中断条件。

for( rowNo = 5; ; rowNo++){ if(sheet.getRow(rowNo).getCell(0).getCellType() == Cell.CELL_TYPE_BLANK || sheet.getRow(rowNo) == null || sheet.getRow(rowNo).getCell(0).getStringCellValue() == ""){ break; } for(int cellNo = 0; cellNo < 5 ; cellNo++){ switch(cellNo){ case 0: if(sheet.getRow(rowNo).getCell(cellNo).getCellType() == Cell.CELL_TYPE_STRING){ productId = formatter.formatCellValue(sheet.getRow(rowNo).getCell(cellNo)); System.out.println(productId); } break; case 1: if(sheet.getRow(rowNo).getCell(cellNo).getCellType() == Cell.CELL_TYPE_STRING){ productName = formatter.formatCellValue(sheet.getRow(rowNo).getCell(cellNo)); System.out.println(productName); } break; case 2: if(sheet.getRow(rowNo).getCell(cellNo).getCellType() == Cell.CELL_TYPE_NUMERIC){ quantity = (int) sheet.getRow(rowNo).getCell(cellNo).getNumericCellValue(); System.out.println(quantity); } break; case 3: if(sheet.getRow(rowNo).getCell(cellNo).getCellType() == Cell.CELL_TYPE_NUMERIC){ price = (int) sheet.getRow(rowNo).getCell(cellNo).getNumericCellValue(); System.out.println(price); } break; case 4: if(sheet.getRow(rowNo).getCell(cellNo).getCellType() == Cell.CELL_TYPE_NUMERIC){ lineTotal = (int) sheet.getRow(rowNo).getCell(cellNo).getNumericCellValue(); System.out.println(lineTotal); } break; } } product.setProductName(productName); product.setQuantity(quantity); product.setPrice(price); product.setLineTotal(lineTotal); productDetails.put(productId, product); } //need to reach here after first loop breaks, instead reaches catch throws NPE. no reason why the NPE. it just throws NPE totalPrice = (int)sheet.getRow(rowNo).getCell(4).getNumericCellValue(); invoice.setInvoiceDate(invoiceDate); invoice.setInvoiceNumber(invoiceNo); invoice.setProductDetails(product); invoice.setProductId(productId); invoice.setRetailerId(retailerNo); invoice.setProductMap(productDetails); invoice.setTotalPrice(totalPrice); return invoice; }catch(Exception e){ e.printStackTrace(); e.getMessage(); } 

如何使用继续命名您的循环:

 loopName : for(){ ... continue loopName; ... }