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

2022-09-04 08:28:48

当我尝试在正常的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 jars。


答案 1

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


答案 2

正如@Jens所说,您需要类路径中该文件。poi-ooxml.jar

您使用的是正确的语句,但 Web 容器的类路径中似乎没有该文件。importpoi-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>

推荐