简体   繁体   English

Java中的Apache POI引起麻烦(XSSFWorkbook)

[英]Apache POI in Java causes trouble (XSSFWorkbook)

I am actually trying to read an XLS file with Apache POI, but my code somehow doesn't work. 我实际上正在尝试使用Apache POI读取XLS文件,但是我的代码不起作用。 IntelliJ tells me that, on line 28, creating the XSSFWorkbook causes the trouble. IntelliJ告诉我,在第28行上,创建XSSFWorkbook会引起麻烦。 Would you have a brief look and maybe answer if you are in this? 如果您处于这种状态,您是否可以简要看一下并回答?

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

How do you add POI to your project? 如何将POI添加到项目中? Do you use Maven or something like that? 您使用Maven还是类似的东西? You might be missing some dependencies. 您可能缺少一些依赖项。

Cut out from my dependency:tree : 从我的依赖项中删除: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? 您的类路径中有所有的jars吗?

Place all the following jars in BuildPath and run! 将以下所有jar放入BuildPath并运行!

在此处输入图片说明

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. 非常感谢您的回复,并祝您愉快。

you need to add these jar files :: 您需要添加以下jar文件::

CLASSPATH: 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-架构 - 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.. 祝你好运..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM