Java 中的 Apache POI 导致问题 (XSSFWorkbook)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21988922/
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-13 11:52:37  来源:igfitidea点击:

Apache POI in Java causes trouble (XSSFWorkbook)

javaapache-poixssf

提问by Paulquappe

I am actually trying to read an XLS file with Apache POI, but my code somehow doesn't work. IntelliJ tells me that, on line 28, creating the XSSFWorkbook causes the trouble. Would you have a brief look and maybe answer if you are in this?

我实际上是在尝试使用 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();
        }
    }
}

采纳答案by Naren

Place all the following jars in BuildPath and run!

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

enter image description here

在此处输入图片说明

回答by Mario B

How do you add POI to your project? Do you use Maven or something like that? You might be missing some dependencies.

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

Cut out from my dependency:tree :

从我的依赖项中删除:树:

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

Do you have all that jars in your classpath?

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

回答by Paulquappe

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

如何使用 poi 在 java 中读取 excel(.xlsx)?

this link and your comments helped me a lot.

这个链接和你的评论对我帮助很大。

i needed to add more jar files to my project.

我需要向我的项目添加更多 jar 文件。

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

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.

有很多回复,祝你有美好的一天。

回答by Srinivas

you need to add these jar files ::

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

CLASSPATH:

类路径:

“C:\poi-3.9\poi-3.9-20121203.jar;”

“C:\poi-3.9\poi-3.9-20121203.jar;”

“C:\poi-3.9\poi-ooxml-3.9-20121203.jar;”

“C:\poi-3.9\poi-ooxml-3.9-20121203.jar;”

“C:\poi-3.9\poi-ooxml-schemas-3.9-20121203.jar;”

“C:\poi-3.9\poi-ooxml-schemas-3.9-20121203.jar;”

“C:\poi-3.9\ooxml-lib\dom4j-1.6.1.jar;”

“C:\poi-3.9\ooxml-lib\dom4j-1.6.1.jar;”

“C:\poi-3.9\ooxml-lib\xmlbeans-2.3.0.jar;”

“C:\poi-3.9\ooxml-lib\xmlbeans-2.3.0.jar;”

Click the below link for the above jar files:: http://www.java2s.com/Open-Source/Java_Free_Code/Database/Download_wca_workbook_assistant_Free_Java_Code.htm

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

Best of Luck..

祝你好运..