apache poi-找到条件后继续循环

时间:2018-10-12 03:22:58

标签: java apache-poi

我有这段代码,现在我想做的是检查此循环是否找到了ABC01,然后它将继续循环以查找“ 2018”。我试图像代码中那样进行检查和递增,但无法正常工作。请帮助我。

public static void ReadFile() throws Exception {
    FileInputStream file = new FileInputStream(new File("C:xx"));
    XSSFWorkbook wb = new XSSFWorkbook(file);
    XSSFSheet sheet = wb.getSheetAt(0);

    for (Iterator<Row> iterator = sheet.rowIterator(); iterator.hasNext();) {
        XSSFRow row = (XSSFRow) iterator.next();
        for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
            XSSFCell cell = row.getCell(i);
            DataFormatter formatter = new DataFormatter();
            String val = formatter.formatCellValue(cell);
            /* System.out.println(val); */

            if (val.equals("ABC01")) {
                i++;
            }
            if (val.equals("2018")) {
                System.out.print("XX");
            }

        }

    }
}

1 个答案:

答案 0 :(得分:1)

根据评论中的讨论,以下代码应为您工作

public static void ReadFile() throws Exception {
FileInputStream file = new FileInputStream(new File("C:xx"));
XSSFWorkbook wb = new XSSFWorkbook(file);
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row = null;
XSSFCell cell = null;

DataFormatter formatter = new DataFormatter();
String val;

for (Iterator<Row> iterator = sheet.rowIterator(); iterator.hasNext();) {
    row = (XSSFRow) iterator.next();

    for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
        cell = row.getCell(i);           
        val = formatter.formatCellValue(cell);
        /* System.out.println(val); */

        if (val.equals("ABC01")) {
            i++;

            cell = row.getCell(i);
            val = formatter.formatCellValue(cell);

            if (val.equals("2018")) {
                System.out.print("XX");
            }
        }


        }

    }
}