java - 如何在不向前移动迭代器的情况下获取单元格值 - Excel、Java POI

标签 java excel apache apache-poi cell

我在使用 java 迭代 excel 中的行和单元格时遇到问题。我以这种方式在工作表中构建数据:

enter image description here

我必须收集所有数字并将其传递给构造函数。我尝试过这样的事情:

Iterator<Row> iterator = firstSheet.iterator();
while (iterator.hasNext()){
         Row nextRow = iterator.next();
         Iterator<Cell> cellIterator = nextRow.cellIterator(); 
         int id = (int) cellIterator.next().getNumericCellValue();
         int pr = (int) cellIterator.next().getNumericCellValue();
         int[] prId = new int[2];

         prId[0] = (int) cellIterator.next().getNumericCellValue();

         iterator.next(); // i have to move down one row to get 5 and 6

         precId[1] = // what should I type here?? cellIterator.next() will cause, that cell with value "5" will be omitted and I will get "6"
         int co = (int) cellIterator.next().getNumericCellValue();
         list.add(new Rule(id, pr, prId, co));

    }

如何处理? 预先感谢:)

编辑:

我决定以其他方式获取细胞:

for(int rowNum = rowStart+1; rowNum < rowEnd; rowNum++){
        Row r = sheet.getRow(rowNum);
        if(r == null){
            continue;
        }
        int lastColumn = Math.max(r.getLastCellNum(), 4);
        for(int cn = 0; cn < lastColumn; cn++){
            @SuppressWarnings("deprecation")
            Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
            if(c == null){
                continue;
            }else{
                System.out.println(c);
            }
        }
    }

这个解决方案显然更好,但我有问题,因为我需要将此值传递给构造函数,而不是通过 System.out.println(c) 显示单元格值,如迭代器的示例所示。但现在我可以使用循环访问单元格,因此我无法使用 .next() 或类似的东西获取下一个单元格的值 - 我无法在一行代码中获取下一个单元格,那么如何创建与上面相同的对象(使用迭代器)但使用此代码?

最佳答案

再次初始化nextRow变量和cellIterator -

Iterator<Row> iterator = firstSheet.iterator();
while (iterator.hasNext()){
         Row nextRow = iterator.next();
         Iterator<Cell> cellIterator = nextRow.cellIterator(); 
         int id = (int) cellIterator.next().getNumericCellValue();
         int pr = (int) cellIterator.next().getNumericCellValue();
         int[] prId = new int[2];

         prId[0] = (int) cellIterator.next().getNumericCellValue();

         nextRow = iterator.next(); // i have to move down one row to get 5 and 6
         cellIterator = nextRow.cellIterator();
         cellIterator.next();   // for first empty cell
         cellIterator.next();   // for second empty cell

         precId[1] = (int) cellIterator.next().getNumericCellValue();
         int co = (int) cellIterator.next().getNumericCellValue();
         list.add(new Rule(id, pr, prId, co));

    }

请注意,在循环中再次调用 iterator.next() 可能会导致一些 RunTimeException,最好先调用 iterator.hasNext(),检查返回值,然后再调用 iterator.next()

关于java - 如何在不向前移动迭代器的情况下获取单元格值 - Excel、Java POI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41524336/

相关文章:

java - 按下按钮时 Android 应用程序崩溃

c# - 使用 Excel Interop w/C# 将 Excel 单元格内容居中

perl - 作为 cgi 脚本运行 Dancer2 应用程序时 HTTP 504 网关超时

sql - Excel 2010 中的 PowerView

facebook - 使用 Nutch 2.1 获取某个页面的 facebook 点赞数

php - 通过 php 运行复杂的 ROS shell 命令

java - created_by 字段上的 DuplicateMappingException

java - JPA 2 - 获取@OneToMany 关系中列表的最后一个元素

java - Liquibase 是否需要写权限才能获取锁?

excel - 公式中的动态列索引名称错误