我的“无法访问的代码”代表了'环

时间:2016-08-24 13:13:59

标签: java

我正在编写一个程序来格式化excel文件的内容。 Eclipse说行Cell cell = cellIterator.next();无法访问,我不明白为什么。我哪里出错了?

private String formatExcel(File excel)
{
    this.statusLabel.setText("formatting...");
    try
    {
        FileInputStream file = new FileInputStream(excel);
        try
        {
            this.workbook = WorkbookFactory.create(file);
        }
        catch (InvalidFormatException ex)
        {
            file.close();
        }
        int excelType = 0;
        if ((this.workbook instanceof HSSFWorkbook)) {
            excelType = 1;
        }
        int sheetNum = 0;
        try
        {
            sheetNum = Integer.parseInt(this.sheetNumber.getText());
        }
        catch (NumberFormatException e)
        {
            file.close();
        }
        if ((sheetNum < 1) || (sheetNum > this.workbook.getNumberOfSheets()))
        {
            file.close();
            return "Please input a valid sheet number.";
        }
        Sheet sheet = this.workbook.getSheetAt(sheetNum - 1);
        sheet.setZoom(17, 20);

        Iterator<Row> rowIterator = sheet.iterator();

        int startRow = Integer.MAX_VALUE;
        Iterator<Cell> cellIterator;
        for (; rowIterator.hasNext(); cellIterator.hasNext())
        {
            Row row = (Row)rowIterator.next();

            cellIterator = row.cellIterator();
            continue;

            Cell cell = (Cell)cellIterator.next(); // <- this line is unreachable
            switch (cell.getCellType())
            {
                case 4: 
                    break;
                case 0: 
                    break;
                case 1: 
                    if (cell.getStringCellValue().trim().equalsIgnoreCase("Condition Code")) {
                        startRow = cell.getRowIndex();
                    }
                    if ((cell.getRowIndex() > startRow + 1) && (cell.getColumnIndex() > 0) && (cell.getColumnIndex() < 5)) {
                        if (excelType == 0) {
                            cell.setCellValue(formatCellXSSF(
                                cell.getStringCellValue(), 
                                cell.getColumnIndex()));
                        } else {
                            cell.setCellValue(formatCellHSSF(
                                cell.getStringCellValue(), 
                                cell.getColumnIndex()));
                        }
                    }
                    if (!cell.getStringCellValue().trim().equalsIgnoreCase("<<Test Data>>")) {
                        if (!cell.getStringCellValue().trim().equalsIgnoreCase("<<Screenshots>>")) {
                            break;
                        }
                    }
                  break;
            }
        }
        sheet.autoSizeColumn(5);

        file.close();

        FileOutputStream out = new FileOutputStream(excel);

        this.workbook.write(out);

        out.close();

        return "";
    }
    catch (FileNotFoundException ex)
    {
        return "Error. File is open. Please close it first.";
    }
    catch (IOException ex) {}
    return "Cannot format file because it is open. Please close it first.";
}

1 个答案:

答案 0 :(得分:2)

您的continue循环中有一个无条件for。永远不会执行下一个语句。

for (; rowIterator.hasNext(); cellIterator.hasNext())
  {
    Row row = (Row)rowIterator.next();

    cellIterator = row.cellIterator();
    continue;

    Cell cell = (Cell)cellIterator.next();