0

我实际上正在尝试使用 Apache POI 读取 XLS 文件,但我的代码不知何故不起作用。IntelliJ 告诉我,在第 28 行,创建 XSSFWorkbook 会导致问题。如果你在这,你会简短地看一下并回答吗?

package Parse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.util.Iterator;
public class poi {

    public static void main(String[] args) {
        try {
            FileInputStream file = new FileInputStream(new File("C:\\Users\\jd\\Desktop\\test\\VW_XML\\in_xls.xlsx"));

            //Create workbook instance
            XSSFWorkbook workbook = new XSSFWorkbook(file);

            //read sheet
            XSSFSheet sheet = workbook.getSheetAt(0);

            //iterate rows
            Iterator<Row> rowIterator = sheet.iterator();
            while (rowIterator.hasNext()) {
                Row row = rowIterator.next();
                Iterator<Cell> cellIterator = row.cellIterator();

                // for each row all columns
                while (cellIterator.hasNext()) {
                    Cell cell = cellIterator.next();

                    //check cell type
                    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();
        }
    }
}
4

4 回答 4

3

您如何将 POI 添加到您的项目中?你使用Maven或类似的东西吗?您可能缺少一些依赖项。

从我的依赖中删除:树:

org.apache.poi:poi-ooxml:jar:3.10-FINAL:compile
 +- org.apache.poi:poi:jar:3.10-FINAL:compile
 |  \- commons-codec:commons-codec:jar:1.5:compile
 \- org.apache.poi:poi-ooxml-schemas:jar:3.10-FINAL:compile
    \- org.apache.xmlbeans:xmlbeans:jar:2.3.0:compile
        \- stax:stax-api:jar:1.0.1:compile

你的类路径中有所有的罐子吗?

于 2014-02-24T13:29:30.507 回答
2

将以下所有 jar 放入 BuildPath 并运行!

在此处输入图像描述

于 2014-02-24T13:29:44.497 回答
1

How to read excel(.xlsx) in java using poi?

this link and your comments helped me a lot.

i needed to add more jar files to my project.

poi-3.9.jar poi-ooxml-3.9.jar poi-ooxml-schemas-3.7.jar xmlbeans-2.3.0.jar dom4j-1.6.1.jar

ty a lot for replys and have a nice day.

于 2014-02-24T14:01:38.723 回答
-1

你需要添加这些jar文件::

类路径:

“C:\poi-3.9\poi-3.9-20121203.jar;”</p>

“C:\poi-3.9\poi-ooxml-3.9-20121203.jar;”</p>

“C:\poi-3.9\poi-ooxml-schemas-3.9-20121203.jar;”</p>

“C:\poi-3.9\ooxml-lib\dom4j-1.6.1.jar;”</p>

“C:\poi-3.9\ooxml-lib\xmlbeans-2.3.0.jar;”</p>

单击以下链接获取上述 jar 文件: http ://www.java2s.com/Open-Source/Java_Free_Code/Database/Download_wca_workbook_assistant_Free_Java_Code.htm

祝你好运。。

于 2017-01-16T04:25:30.017 回答