如何在Apache的POI使用rowiterator与Java? [英] How to use rowiterator in apache poi with java?

查看:893
本文介绍了如何在Apache的POI使用rowiterator与Java?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取使用Apache POI在Java中的Excel文件,但是,Eclipse中没有编译code。

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

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中始终强调鳞次栉比= rowIterator()和Next(); 行。我不知道为什么?我怎么能提高呢?

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

推荐答案

问题是不是日食,它与code。你不能把rowIterator其是可变的,作为一种方法。你不能调用与()语法的变量。

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.

试试这个:

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

这篇关于如何在Apache的POI使用rowiterator与Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆