在解析excel表时,应该有什么样的中断条件才能脱离for循环


  • 我有一张excel表。我正在使用javaPOI对其进行解析。

  • 我需要在for循环的帮助下解析第6-15行。

  • 在第16行,第一个单元格为空,第三行和第四行里面有值。那么爆发的条件应该是什么当计数器到达第16行时循环?

注意:迭代值将根据不同的excel工作表而变化。因此,我需要一个一般的for循环中断条件

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;
    ...
}

最新更新