注意:对于不同的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();
}
发布于 2016-05-04 18:49:48
如何使用继续字命名您的循环:
loopName : for(){
...
continue loopName;
...
}
https://stackoverflow.com/questions/37035586
复制相似问题