1

我一直在使用 Apache POI 并使用我的这段代码:

private void remove() {
    HSSFWorkbook wb = null;
    HSSFSheet sheet = null;
    try {
        FileInputStream is = new FileInputStream("C:/juni.xls");
        wb = new HSSFWorkbook(is);
        sheet = wb.getSheetAt(0);
        for (Row row : sheet) {
            for (Cell cell : row) {
                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    if (cell.getRichStringCellValue().getString().trim().contains("POST")) {
                        row_count_post_1 = row.getRowNum();
                        DashBoard.txt.setText("Processed the STRING \"POST\" on Row#" + row_count_post_1);
                        HSSFRow removingRow = sheet.getRow(row_count_post_1);
                        if (removingRow != null) {
                            sheet.removeRow(removingRow);
                        }
                        int lastIndex = sheet.getLastRowNum();
                        sheet.shiftRows(row_count_post_1 + 1, lastIndex, -1);
                    } else {
                        break;
                    }////////////want to break from here;
                }
            }
        }
    } catch (Exception e) {
        //do catch for no exception
    }
}

if问题是当我的语句停止工作时我想退出循环。如果我不使用我的else. 实际上,如果此代码无法找到一个字符串,它应该停止,但在我的情况下,else 突然与我的 if 一起工作,在这种情况下只会发生一次迭代。请告诉我我做错了什么,我只需要提示而不是整体帮助。提前致谢

4

4 回答 4

2

您的问题尚不清楚,但请探索:

private void remove(){
    HSSFWorkbook wb = null;
    HSSFSheet sheet=null;

    try {

        FileInputStream is = new FileInputStream("C:/juni.xls");

        wb = new HSSFWorkbook(is);
        sheet = wb.getSheetAt(0);
        //Tagging the loops
        OuterLoop : for (Row row: sheet) {
            InnerLoop : for (Cell cell: row) {

                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {

                    if (cell.getRichStringCellValue().getString().trim().contains("POST")) {

                        row_count_post_1 = row.getRowNum();

                        DashBoard.txt.setText("Processed the STRING \"POST\" on Row#" + row_count_post_1);
                        HSSFRow removingRow = sheet.getRow(row_count_post_1);

                        if (removingRow != null) {
                            sheet.removeRow(removingRow);
                        }
                        int lastIndex = sheet.getLastRowNum();
                        sheet.shiftRows(row_count_post_1 + 1, lastIndex, -1);
                    } else {
                        break InnerLoop; // OR break OuterLoop;
                    } ////////////want to break from here;
                }
            }
        }
    } catch (Exception e) {
        //do catch for no exception
    }
}
于 2013-06-24T08:30:07.980 回答
2

您可以将break标签与外部循环一起使用。

请看一下@jon-skeet 的 回答或javadoc 中的这个例子

于 2013-06-24T08:32:56.197 回答
1

由于在完成您想要的第一个循环后没有代码break,只需return.

} else {
    return;
}

尽管标签和断到标签是有效的语法,但我会避免这种流控制。根据 Dijkstra 的说法,代码中的一些错误与goto 语句的数量成正比(这个 break 是它的一个版本)。我只想说:小心点。如果您必须有类似的流控制,请尝试使用标志来实现它。

于 2013-06-24T08:32:23.117 回答
0

First of all please use Iterator for your loops (See Ref : http://viralpatel.net/blogs/java-read-write-excel-file-apache-poi/)

Second: You may have to do like this to stop your loop.

private void remove() {
    HSSFWorkbook wb = null;
    HSSFSheet sheet = null;
    try {
        FileInputStream is = new FileInputStream("C:/juni.xls");
        wb = new HSSFWorkbook(is);
        sheet = wb.getSheetAt(0);
        for (Row row : sheet) {
            boolean stop = false;
            for (Cell cell : row) {
                if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                    if (cell.getRichStringCellValue().getString().trim().contains("POST")) {
                        row_count_post_1 = row.getRowNum();
                        DashBoard.txt.setText("Processed the STRING \"POST\" on Row#" + row_count_post_1);
                        HSSFRow removingRow = sheet.getRow(row_count_post_1);
                        if (removingRow != null) {
                            sheet.removeRow(removingRow);
                        }
                        int lastIndex = sheet.getLastRowNum();
                        sheet.shiftRows(row_count_post_1 + 1, lastIndex, -1);
                    } else {
                        stop = true;
                        break;
                    }
                }
                if (stop) {
                    break;
                }
            }
        }
    } catch (Exception e) {
        //do catch for no exception
    }
}
于 2013-06-24T08:30:51.547 回答