java.lang.ClassNotFoundException:org.apache.poi.xssf.usermodel.XSSFWorkbook

时间:2014-11-18 11:12:18

标签: java servlets apache-poi xlsx xssf

当我试图在正常的java类中创建一个工作簿时,它正在创建但是当我试图在servlet上运行相同时它会抛出异常

普通的java类

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class Sampleclass {

    public static void main(String[] args) {

        FileInputStream file;
        try {
            file = new FileInputStream(new File("C:\\Users\\Rajasekhar\\Desktop\\EmployeeDetails.xlsx"));
            Workbook book=new XSSFWorkbook(file);
            System.out.println(book);

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

的Servlet

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


@SuppressWarnings("serial")
public class sampleServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
             {
        FileInputStream file;
        try {
            file = new FileInputStream(new File("C:\\Users\\Rajasekhar\\Desktop\\EmployeeDetails.xlsx"));
            Workbook book=new XSSFWorkbook(file);

        } catch (FileNotFoundException e) {
            System.out.println("exception occured is "+e);      
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("exception occured is "+e);      
            e.printStackTrace();
        }
    }

}

发生的异常是

SEVERE: Servlet.service() for servlet [upload] in context with path [/Temp2] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.apache.poi.xssf.usermodel.XSSFWorkbook
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
    at sampleServlet.doGet(sampleServlet.java:23)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:315)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

我添加了所有必需的apache poi jar。

5 个答案:

答案 0 :(得分:6)

下载poi-ooxml.jar并将其添加到您的类路径中。

答案 1 :(得分:2)

您需要在类路径中使用文件poi-ooxml.jar@Jens表示。

您使用了正确的import声明,但似乎您的网络容器的类路径中没有文件poi-ooxml.jar

如果您使用 maven ,则应将此依赖项放在pom.xml标记内的<dependencies>中:

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

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

答案 2 :(得分:0)

我知道这可能会迟到但问题是您可能会忽略文件xmlbeans-2.6.0.jar,该文件位于与其他apache jar(ooxml-lib)不同的文件中

答案 3 :(得分:0)

在Apache Poi的Gradle依赖项下添加/更新将解决此问题。

compile group: 'org.apache.poi', name: 'poi', version: '4.1.2'
compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.1.2'

答案 4 :(得分:0)

如果您正确导入并添加了 poi 和 poi-ooxml 等 jar 文件,但仍然遇到问题,请尝试此版本

它的工作......并希望您的问题得到解决。

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