淘先锋技术网

首页 1 2 3 4 5 6 7

异常

You need to add dependency of 'poi-ooxml' to your project, and version >= 3.17

cn.hutool.core.exceptions.DependencyException: You need to add dependency of 'poi-ooxml' to your project, and version >= 3.17
        at cn.hutool.poi.excel.ExcelUtil.getReader(ExcelUtil.java:310)
        at cn.hutool.poi.excel.ExcelUtil.getReader(ExcelUtil.java:261)
        at c.h.u.a.c.UC.impoetFile(UC.java:306)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
        at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
        at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
        ...
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.apache.poi.xssf.model.SharedStringsTable
        at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:61)
        at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:660)
        at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:165)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:278)
        at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:118)
        at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.create(XSSFWorkbookFactory.java:98)
        at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.create(XSSFWorkbookFactory.java:36)
        at org.apache.poi.ss.usermodel.WorkbookFactory.lambda$create$2(WorkbookFactory.java:199)
        at org.apache.poi.ss.usermodel.WorkbookFactory.wp(WorkbookFactory.java:295)
        at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:199)
        at cn.hutool.poi.excel.WorkbookUtil.createBook(WorkbookUtil.java:88)
        at cn.hutool.poi.excel.WorkbookUtil.createBook(WorkbookUtil.java:74)
        at cn.hutool.poi.excel.ExcelReader.<init>(ExcelReader.java:85)
        at cn.hutool.poi.excel.ExcelUtil.getReader(ExcelUtil.java:308)

jar版本 

 <!--hutool-->

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>4.5.10</version>
</dependency>

<!--poi-->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>5.0.0</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>4.1.2</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.0.0</version>
</dependency>

原因 

hutool-all版本太低,升级到5.*

后续问题

hutool-all升级后,报

SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: 'org.apache.xmlbeans.XmlOptions org.apache.xmlbeans.XmlOptions.setUseDefaultNamespace(boolean)'] with root cause
java.lang.NoSuchMethodError: 'org.apache.xmlbeans.XmlOptions org.apache.xmlbeans.XmlOptions.setUseDefaultNamespace(boolean)'
        at org.apache.poi.xssf.model.SharedStringsTable.<clinit>(SharedStringsTable.java:96)
        at org.apache.poi.ooxml.POIXMLFactory.createDocumentPart(POIXMLFactory.java:61)
        at org.apache.poi.ooxml.POIXMLDocumentPart.read(POIXMLDocumentPart.java:660)
        at org.apache.poi.ooxml.POIXMLDocument.load(POIXMLDocument.java:165)
        at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:278)
        at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:118)
        at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.create(XSSFWorkbookFactory.java:98)
        at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.create(XSSFWorkbookFactory.java:36)
        at org.apache.poi.ss.usermodel.WorkbookFactory.lambda$create$2(WorkbookFactory.java:199)
        at org.apache.poi.ss.usermodel.WorkbookFactory.wp(WorkbookFactory.java:295)
        at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:199)
        at cn.hutool.poi.excel.WorkbookUtil.createBook(WorkbookUtil.java:110)
        at cn.hutool.poi.excel.WorkbookUtil.createBook(WorkbookUtil.java:97)
        at cn.hutool.poi.excel.ExcelReader.<init>(ExcelReader.java:86)
        at cn.hutool.poi.excel.ExcelUtil.getReader(ExcelUtil.java:208)
        at cn.hutool.poi.excel.ExcelUtil.getReader(ExcelUtil.java:195)
        at c.h.u.a.c.UC.impoetFile(UC.java:306)
...

原因

poi-ooxml-schemas包引入错误,poi包5.*版本更改了名称:poi-ooxml-schemas 改为 poi-ooxml-lite/full;

删除poi-ooxml-schemas包,poi-ooxml-lite包已经在poi包里,所以只添加poi-ooxml-full

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

 解析Excel正常;

参考(poi官方文档5.0.0栏):History of Changes