如何使用java在apache poi中使用rowiterator?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19280956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 15:40:27  来源:igfitidea点击:

How to use rowiterator in apache poi with java?

javarowapache-poihssf

提问by Aleksei Nikolaevich

I tried to read an excel file using apache poi in java, however, Eclipse did not compile the code.

我试图在java中使用apache poi读取excel文件,但是,Eclipse没有编译代码。

public class ReadExcel {

    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();      \ THIS LINE GETS UNDERLINED BY ECLIPSE!!!
         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();
        }


    }

Eclipse always underlines Row row = rowIterator().next();line. I do not know why? How can I improve it?

Eclipse 总是下划线Row row = rowIterator().next();。我不知道为什么?我该如何改进?

采纳答案by axiopisty

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.

问题不在于 eclipse,而在于代码。您不能将作为变量的 rowIterator 视为方法。您不能使用 () 语法调用变量。

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();
  }

回答by rgettman

In this line:

在这一行:

Row row = rowIterator().next();

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

您正在尝试调用rowIterator在您自己的类上调用的方法,当然您没有。

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

从上下文中可以看出,您的意思是指rowIterator您已经拥有的变量。将其更改为:

Row row = rowIterator.next();  // No () on rowIterator

回答by Mike Bockus

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

您需要在 rowIterator 之后删除“()”。

Instead of:

代替:

rowIterator().next();

It should be:

它应该是:

rowIterator.next()