Read from excel file .xlsx using java Apache POI 3

2019-05-02 05:21发布

问题:

I am trying to read a file from a .xlsx file using java. But I still get errors. I already corrected the HSSF to XSSF so it is able to read past 2007 version of excel. The code crashes when instantiating the workbook. Here is the code:

package excelread;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ReadExcel {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File excel =  new File ("C:/Users/Leah-Dina/Desktop/LogFile.xlsx");
        FileInputStream fis = new FileInputStream(excel);
        XSSFWorkbook wb = new XSSFWorkbook(fis);
        XSSFSheet ws = wb.getSheet("Input");

        int rowNum = ws.getLastRowNum() + 1;
        int colNum = ws.getRow(0).getLastCellNum();
        String [][] data = new String [rowNum] [colNum];

        for(int i = 0; i <rowNum; i++){
            XSSFRow row = ws.getRow(i);
                for (int j = 0; j < colNum; j++){
                    XSSFCell cell = row.getCell(j);
                    String value = cell.toString();
                    data[i][j] = value;
                    System.out.println ("the value is " + value);
                }
        }

    }
}

Here you can see the error message I get: Seems like everything is imported and I have no idea whats wrong.

 Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
        at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:154)
        at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:141)
        at org.apache.poi.openxml4j.opc.Package.<init>(Package.java:54)
        at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:82)
        at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:267)
        at org.apache.poi.util.PackageHelper.open(PackageHelper.java:39)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:204)
        at excelread.ReadExcel.main(ReadExcel.java:21)
    Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 8 more

回答1:

First, make sure that all libraries that Apache POI depends on are on your classpath. In this case, you are certainly missing Dom4J (dom4j-1.6.1.jar). Possibly you may be missing other libraries, such as stax-api-1.0.1.jar, xmlbeans-2.3.0.jar, and poi-ooxml-schemas-3.9.jar. All necessary libraries are included in the distribution that is downloadable from the Apache POI website.

Line 21 appears to be this line:

XSSFWorkbook wb = new XSSFWorkbook(fis);

So there may be a problem with your spreadsheet. Putting Dom4J on your classpath will only allow the DocumentException to be created, but hopefully that will tell you what's really wrong with your spreadsheet (if anything).



回答2:

You should include dom4j-1.6.1.jar file



回答3:

simply paste your poi libs in your web-inf lib directory if you are working in eclipse IDE.