首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >解析excel表时,应该打破哪些条件才能脱离for循环?

解析excel表时,应该打破哪些条件才能脱离for循环?
EN

Stack Overflow用户
提问于 2016-05-04 18:43:32
回答 1查看 70关注 0票数 1
  • 我有一张表格。我正在使用java解析它。
  • 我需要使用for循环来解析行6-15。
  • 在第16行,第一个单元格为空,第三行和第四行的值在其中。那么,当计数器到达第16行时,应该有什么条件来断开循环呢?

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

代码语言:javascript
运行
复制
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();
    }
EN

回答 1

Stack Overflow用户

发布于 2016-05-04 18:49:48

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

代码语言:javascript
运行
复制
loopName : for(){

    ...
    continue loopName;
    ...

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37035586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档