2015-09-05 48 views
0

我正在嘗試使用Apache POI讀取.xlsx文件。我加入到構建路徑的jar文件:HTMLDocument.Iterator類型不是通用錯誤

 
poi-3.12-20150511.jar 
poi-ooxml-3.12-20150511.jar 
poi-ooxml-schemas-3.12-20150511.jar 
xmlbeans-2.6.0.jar

我現在用的這個readFromExcel方法:

public void readFromExcel(){ 


    String excelFilePath = "Books.xlsx"; 
    FileInputStream inputStream = new FileInputStream(new File(excelFilePath)); 


    XSSFWorkbook workbook = new XSSFWorkbook(inputStream); 

    XSSFSheet firstSheet = workbook.getSheetAt(0); 

    Iterator<Row> iterator = firstSheet.iterator(); 


     while (iterator.hasNext()) { 
      Row nextRow = iterator.next(); 
      Iterator<Cell> cellIterator = nextRow.cellIterator(); 

      while (cellIterator.hasNext()) { 
       Cell cell = cellIterator.next(); 

       switch (cell.getCellType()) { 
        case Cell.CELL_TYPE_STRING: 
         System.out.print(cell.getStringCellValue()); 
         break; 
        case Cell.CELL_TYPE_BOOLEAN: 
         System.out.print(cell.getBooleanCellValue()); 
         break; 
        case Cell.CELL_TYPE_NUMERIC: 
         System.out.print(cell.getNumericCellValue()); 
         break; 
       } 
       System.out.print(" - "); 
      } 
      System.out.println(); 
     } 

     workbook.close(); 
     inputStream.close(); 

現在我流汗這兩個錯誤:

類型HTMLDocument的.Iterator不是通用的;它不能用參數參數化 HTMLDocument.Iterator類型不是通用的;它不能用參數參數化

+0

你可以發佈你的進口?只有它看起來像你輸入了錯誤的類型 – Gagravarr

回答

0

您可以使用row.getCell()方法。 試試這個:

Workbook workbook = WorkbookFactory.create(yourFile.getInputstream()); 

Sheet sheet = workbook.getSheet(0);//1,2,3 

Iterator<Row> rowIterator = sheet.iterator(); 

     while (rowIterator.hasNext()) { 

      Row row = rowIterator.next(); 

      row.getCell(0); 
      row.getCell(1); 
} 
0

您已經導入了錯誤的迭代器!

在你的導入部分,你必須有這樣的:

import blah.blah.HTMLDocument.Iterator; 

您需要更改爲正確的一個,java.util.Iterator,如

import java.util.Iterator; 

你還做了一些其他的錯誤太...因爲explained here in the POI docs,如果你有一個File,給那個POI,而不是FileInputStream

其次,你可以使用,每次迭代,爲detailed here in the docs,如

for (Row row : sheet) { 
    for (Cell cell : row) { 
     ... 

然後,你甚至不需要明確地使用一個迭代開始!

相關問題