从excel表读取时在迭代器中获取错误

时间:2017-07-04 16:05:53

标签: java

即使我已导入

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

我在部分

收到错误

Iterator<Cell> cellIterator=row.cellIterator();

这是我的完整代码

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
//import org.apache.poi.hssf.record.formula.functions.Cell;
//import org.apache.poi.hssf.record.formula.functions.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class UploadExcel {

    public static void main(String[] args) {
        try {
            FileInputStream file = new FileInputStream(new File("C:\\Documents and Settings\\admin\\Desktop\\imp data\\howtodoinjava_demo.xlsx"));

            //Create Workbook instance holding reference to .xlsx file
            XSSFWorkbook workbook = new XSSFWorkbook(file);

            //Get first/desired sheet from the workbook
            XSSFSheet sheet = workbook.getSheetAt(0);

            //Iterate through each rows one by one
            Iterator<Row> rowIterator = sheet.iterator();

            while (rowIterator.hasNext())
            {
                Row row = rowIterator.next();

                //For each row, iterate through all the columns
                Iterator<Cell> cellIterator=row.cellIterator();

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

                     //Check the cell type and format accordingly
                     switch (cell.getCellType()) 
                     {
                         case Cell.CELL_TYPE_NUMERIC:
                         System.out.print(cell.getNumericCellValue() + "\t");
                         break;
                         case Cell.CELL_TYPE_STRING:
                         System.out.print(cell.getStringCellValue() + "\t");
                         break;
                     }
                 }

                 System.out.println("");
             }

             file.close();
         } catch (Exception e) {
             e.printStackTrace();
         }
     }
 }

我得到的错误是:

Type mismatch: cannot convert from java.util.Iterator<org.apache.poi.ss.usermodel.Cell> to java.util.Iterator<org.apache.poi.hssf.record.formula.functions.Cell>

你能帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

您还需要xssf的这种依赖关系:

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml</artifactId>
  <version>3.16</version>
</dependency>

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.16</version>
</dependency>

<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-ooxml-schemas</artifactId>
  <version>3.7</version>
</dependency>

如果您现在没有收到编译错误,则表示您从其他地方获得此依赖关系,可能您的RiverBoat项目暴露了较旧/不兼容的版本。所以你要么在这个pom中都需要poi依赖,要么都没有(因为两者都可能被RiverBoat暴露)。