How to use rowiterator in apache poi with java?

淺唱寂寞╮ 提交于 2019-12-04 09:05:21

The problem is not with eclipse, it is with the code. You can not treat rowIterator which is a variable, as a method. You can not invoke a variable with the () syntax.

Try this:

  public static void main(String[] args) throws IOException {
    FileInputStream file = new FileInputStream(new File("C:\\Users\\XXXXXXXXXXXXXXXXal\\042012.xls"));
    HSSFWorkbook wb = new HSSFWorkbook(file);
    HSSFSheet sheet = wb.getSheetAt(0);
    Iterator<Row> rowIterator = sheet.iterator();
    while (rowIterator.hasNext()) {
      Row row = rowIterator.next();
      Iterator <Cell> cellIterator = row.cellIterator();
      while (cellIterator.hasNext()) {
        Cell cell = cellIterator.next();
        System.out.print(cell.getStringCellValue() + "\t\t");
      }
    }
    file.close();
    FileOutputStream out =
      new FileOutputStream(new File("C:\\test.xls"));
    wb.write(out);
    out.close();
  }

You need to remove the "()" after rowIterator.

Instead of:

rowIterator().next();

It should be:

rowIterator.next()

In this line:

Row row = rowIterator().next();

You are attempting to call a method called rowIterator on your own class, which of course you don't have.

From the context it's clear you meant to refer to the rowIterator variable you already have. Change it to:

Row row = rowIterator.next();  // No () on rowIterator
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!