[alibaba/easyexcel]本地程序一切正常,web环境报错WorkbookDocumentImpl cannot be cast to WorkbookDocument

2024-06-20 909 views
7

经过测试1.1.2-beta4发现

本地程序一切正常

环境eclipse+jdk8,解析服务器端路径文件xlsx文件一切正常,使用如下方法

    public static <T extends BaseRowModel> List<T> readExcelWithModel(String fileurl, Class<T> t) {
        List<T> datas = new ArrayList<>();
        try (InputStream inputStream = new BufferedInputStream(new FileInputStream(fileurl));) {
            // 解析每行结果在listener中处理
            ExcelListener<T> listener = new ExcelListener<>();
            ExcelReader excelReader = new ExcelReader(inputStream, null, listener);
            excelReader.read(new Sheet(1, 1, t));
            datas = listener.getDatas();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return datas;

    }
web环境报错

eclipse下jfinal3.5搭建web环境,启动jetty之后,通过controller调用同一个方法访问服务器端的同一个xlsx文件,直接报异常

[ERROR]-[Thread: qtp2092769598-17]-[com.jfinal.core.ActionHandler.handle()]: /yftest
com.alibaba.excel.exception.ExcelAnalysisException: File type error,io must be available markSupported,you can do like this <code> new BufferedInputStream(new FileInputStream(\"/xxxx\"))</code> "
    at com.alibaba.excel.analysis.ExcelAnalyserImpl.getSaxAnalyser(ExcelAnalyserImpl.java:58)
    at com.alibaba.excel.analysis.ExcelAnalyserImpl.analysis(ExcelAnalyserImpl.java:72)
    at com.alibaba.excel.analysis.ExcelAnalyserImpl.analysis(ExcelAnalyserImpl.java:67)

跟踪发现源头是在类com.alibaba.excel.analysis.v07.XlsxSaxAnalyser

public XlsxSaxAnalyser(AnalysisContext analysisContext) throws IOException, OpenXML4JException, XmlException {
        this.analysisContext = analysisContext;
        analysisContext.setCurrentRowNum(0);
        this.xssfReader = new XSSFReader(OPCPackage.open(analysisContext.getInputStream()));
        this.sharedStringsTable = this.xssfReader.getSharedStringsTable();

        InputStream workbookXml = xssfReader.getWorkbookData();
        WorkbookDocument ctWorkbook = WorkbookDocument.Factory.parse(workbookXml);

48行处报异常 org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.WorkbookDocumentImpl cannot be cast to org.openxmlformats.schemas.spreadsheetml.x2006.main.WorkbookDocument 求助大家,看看有无遇到过同样的问题?

补充

升级poi之后问题不再出现

                <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-compress</artifactId>
            <version>1.18</version>
        </dependency> 

回答

5

应该是你包冲突导致的,目前easyexcel里面依赖版本是3.17。4.0版本我还需要验证一下再做升级

5

应该是你包冲突导致的,目前easyexcel里面依赖版本是3.17。4.0版本我还需要验证一下再做升级

谢谢回复!我研究一下看看那个包导致的

9

应该是你包冲突导致的,目前easyexcel里面依赖版本是3.17。4.0版本我还需要验证一下再做升级 对1.1.2-beta4的文件src/main/java/com/alibaba/excel/analysis/v07/XlsxSaxAnalyser.java 做了如下修改 // WorkbookDocument ctWorkbook = WorkbookDocument.Factory.parse(workbookXml); // CTWorkbook wb = ctWorkbook.getWorkbook(); CTWorkbook wb = CTWorkbook.Factory.parse(workbookXml); 此问题解决。 不知道对原有设计是否有其他影响。