简体   繁体   中英

java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlOptions

I am trying to use POI in a servlet to process an uploaded file as Excel file:

public static String readExcel(InputStream inp) {
    // InputStream inp = null;
    StringBuilder excelDataBuilder = new StringBuilder();
    try {
        Workbook wb = WorkbookFactory.create(inp);
        Sheet sheet = wb.getSheetAt(0);
        Header header = sheet.getHeader();

        int rowsCount = sheet.getLastRowNum();
        for (int rowCounter = 0; rowCounter <= rowsCount; rowCounter++) {
            Row row = sheet.getRow(rowCounter);
            int colCounts = row.getLastCellNum();
            for (int colCounter = 0; colCounter < colCounts; colCounter++) {
                Cell cell = row.getCell(colCounter);
                excelDataBuilder.append(cell.getStringCellValue());
                if (colCounter < colCounts)
                    excelDataBuilder.append(",");
            }
            if (rowCounter <= rowsCount) {
                excelDataBuilder.append("\n");
            }
        }
        return excelDataBuilder.toString();

    } catch (Exception ex) {
        LOG.error("Exception", ex);
    } finally {
        try {
            inp.close();
        } catch (IOException ex) {
            LOG.error("IOException", ex);
        }
    }
    return excelDataBuilder.toString();
}

However, it threw an exception at the following line:

Workbook wb = WorkbookFactory.create(inp);

Here's the Stack Trace:

SEVERE: Servlet.service() for servlet [com.bhrt93.excel.servlet.UploadProcessExcel] in context with path [/ExcelServletProcessCommons] threw exception [Servlet execution threw an exception] with root cause
java.lang.ClassNotFoundException: org.apache.xmlbeans.XmlOptions
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    at org.apache.poi.POIXMLDocumentPart.<clinit>(POIXMLDocumentPart.java:59)
    at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:78)
    at com.bhrt93.excel.service.ExcelReaderService.readExcel(ExcelReaderService.java:22)
    at com.bhrt93.excel.servlet.UploadProcessExcel.doPost(UploadProcessExcel.java:69)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
    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:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    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:316)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)

How is this caused and how can I solve it?

As detailed in the Apache POI page on Components and their Dependencies ...

If you want to use Common SS code, such as WorkbookFactory, you need both the poi and poi-ooxml POI jars on your classpath. In addition, you also need their dependencies too. One of the listed dependencies is xmlbeans , which incidentally is where your missing class comes from

So, you just need to review the Components and Dependencies page , then make sure you include the appropriate jars from the binary package of Apache POI on your classpath. (Or use Maven and let it worry about that for you)

如此处所示 ,您必须添加(至少在我的情况下就是这样): xmlbeans-2.6.0.jar

System couldn't find the XML beans dependency on your classpath so you should add the XML beans dependency to your class path.

The XML beans library looks like xmlbeans-xxxjar

https://mvnrepository.com/artifact/org.apache.xmlbeans/xmlbeans

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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