[alibaba/easyexcel]1.1.1版本读文件,使用ExcelReader不同构造操作时有的报错

2024-06-20 610 views
8

我分别使用了里面的5种构造方法,在1.1.1版本中有三个方法标识过时,这两个方法可以完成文件读取操作;但是,目前未过时的两个构造却不能完成操作。查看了源码,发现跟过时的两个构造方法是对应的,只是获取ExcelTypeEnum的方式不同,但却报错: 测试代码: ` @Test public void importExcel() throws IOException {

    try (InputStream inputStream = new FileInputStream(BASE_PATH + "scheduling.xlsx")) {
        MyExcelListener excelListener = new MyExcelListener();
        // AnalysisParam param = new AnalysisParam(inputStream, ExcelTypeEnum.XLSX, excelListener);
        // ExcelReader reader = new ExcelReader(param, excelListener);
        ExcelReader reader = new ExcelReader(inputStream, null, excelListener);
        // ExcelReader reader = new ExcelReader(inputStream, null, excelListener,true);
        // ExcelReader reader = new ExcelReader(inputStream, ExcelTypeEnum.XLSX, null, excelListener);
        // ExcelReader reader = new ExcelReader(inputStream, ExcelTypeEnum.XLSX, null, excelListener,true);

        List<Sheet> sheets = reader.getSheets();
        for (Sheet sheet : sheets) {
            reader.read(sheet);
        }

        return;
    }
} `

错误如下: ` com.alibaba.excel.exception.ExcelAnalysisException: Analyse excel occur file error fileType XLSX

at com.alibaba.excel.analysis.ExcelAnalyserImpl.getSaxAnalyser(ExcelAnalyserImpl.java:34)
at com.alibaba.excel.analysis.ExcelAnalyserImpl.getSheets(ExcelAnalyserImpl.java:61)
at com.alibaba.excel.ExcelReader.getSheets(ExcelReader.java:114)
at com.ihaohuan.excel.ExcelTest.importExcel(ExcelTest.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestExecutionCallbacks.evaluate(RunBeforeTestExecutionCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestExecutionCallbacks.evaluate(RunAfterTestExecutionCallbacks.java:84)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Caused by: org.apache.poi.EmptyFileException: The supplied file was empty (zero bytes long) at org.apache.poi.util.IOUtils.peekFirstNBytes(IOUtils.java:74) at org.apache.poi.util.IOUtils.peekFirst8Bytes(IOUtils.java:57) at org.apache.poi.poifs.filesystem.FileMagic.valueOf(FileMagic.java:135) at org.apache.poi.openxml4j.opc.internal.ZipHelper.verifyZipHeader(ZipHelper.java:175) at org.apache.poi.openxml4j.opc.internal.ZipHelper.openZipStream(ZipHelper.java:209) at org.apache.poi.openxml4j.opc.ZipPackage.(ZipPackage.java:98) at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:324) at com.alibaba.excel.analysis.v07.XlsxSaxAnalyser.(XlsxSaxAnalyser.java:44) at com.alibaba.excel.analysis.ExcelAnalyserImpl.getSaxAnalyser(ExcelAnalyserImpl.java:31) ... 33 more `

几个构造方法: ` // 可以完成 @Deprecated public ExcelReader(InputStream in, ExcelTypeEnum excelTypeEnum, Object customContent, AnalysisEventListener eventListener) { this(in, excelTypeEnum, customContent, eventListener, true); }

// 不能完成
public ExcelReader(InputStream in, Object customContent,
                   AnalysisEventListener eventListener) {
    this(in, customContent, eventListener, true);
}

// 可以完成
@Deprecated
public ExcelReader(AnalysisParam param, AnalysisEventListener eventListener) {
    this(param.getIn(), param.getExcelTypeEnum(), param.getCustomContent(), eventListener, true);
}

// 可以完成
@Deprecated
public ExcelReader(InputStream in, ExcelTypeEnum excelTypeEnum, Object customContent,
                   AnalysisEventListener eventListener, boolean trim) {
    validateParam(in, excelTypeEnum, eventListener);
    analyser.init(in, excelTypeEnum, customContent, eventListener, trim);
}

// 不能完成
public ExcelReader(InputStream in, Object customContent,
                   AnalysisEventListener eventListener, boolean trim) {
    ExcelTypeEnum excelTypeEnum = ExcelTypeEnum.valueOf(in);
    validateParam(in, excelTypeEnum, eventListener);
    analyser.init(in, excelTypeEnum, customContent, eventListener, trim);
}`

回答

1

我也碰到了这个问题, 测试了以下三种情况, 如果使用未过期的方法, 则要套上 BufferedInputStream

  • FileInputStream + 指定文件类型, 可以读取(使用过期的构造方法)
  • BufferedInputStream(FileInputStream) + 不指定文件类型, 可以读取
  • FileInputStream + 不指定文件类型, 抛出异常
      com.alibaba.excel.exception.ExcelAnalysisException: Analyse excel occur file error fileType XLSX
        at com.alibaba.excel.analysis.ExcelAnalyserImpl.getSaxAnalyser(ExcelAnalyserImpl.java:34)
        at com.alibaba.excel.analysis.ExcelAnalyserImpl.analysis(ExcelAnalyserImpl.java:53)
        at com.alibaba.excel.ExcelReader.read(ExcelReader.java:92)
        at com.loyofo.test.excelTest.ExcelTest1.test2(ExcelTest1.java:157)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
        at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
      Caused by: org.apache.poi.openxml4j.exceptions.NotOfficeXmlFileException: No valid entries or contents found, this is not a valid OOXML (Office Open XML) file
        at org.apache.poi.openxml4j.opc.ZipPackage.getPartsImpl(ZipPackage.java:286)
        at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:756)
        at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:327)
        at com.alibaba.excel.analysis.v07.XlsxSaxAnalyser.<init>(XlsxSaxAnalyser.java:44)
        at com.alibaba.excel.analysis.ExcelAnalyserImpl.getSaxAnalyser(ExcelAnalyserImpl.java:31)
        ... 25 more

具体代码如下,

    @Test
    public void test1() throws Exception {
        // FileInputStream + 指定文件类型, 可以读取
        InputStream inputStream = new FileInputStream("TestData/excel/221.xlsx");

        try {
            ExcelReader reader = 
                new ExcelReader(inputStream, ExcelTypeEnum.XLSX, null, new AnalysisEventListener<List<String>>() {...});

            reader.read();
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test2() throws Exception {
        // BufferedInputStream(FileInputStream) + 不指定文件类型, 可以读取
        InputStream inputStream = new BufferedInputStream(new FileInputStream("TestData/excel/221.xlsx"));

        // FileInputStream + 不指定文件类型, 抛出异常:
        // InputStream inputStream = new FileInputStream("TestData/excel/221.xlsx");
        try {
            ExcelReader reader = 
                new ExcelReader(inputStream, null, new AnalysisEventListener<List<String>>() {...});

            reader.read();
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
8

如果是jdk>=1.8的话可以替换成poi4.0.0即可解决,有可能是poi的bug导致的

  • org.apache.poi poi-ooxml 4.0.0
    <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>
2

https://github.com/alibaba/easyexcel/pull/121

FileMagic.valueOf需要读取8个byte来判定文件类型,这个peek操作会影响原始的inputStream,导致后续真正读取文件的时候报错

bug修正之前可以传 BufferedInputStream 进去就不会有问题

7

@vivisidea 正解,问题应该就是出在这里。

0

ExcelReader excelReader = EasyExcelFactory.getReader(inputStream,excelListener); List sheets = excelReader.getSheets();

6

1.1.2-beta2处理了这个问题,但是如果是03版excel需要在输入流左一层new BufferedInputStream(new FileInputStream("/xxxx/xxx/77.xls")确保markSupported 返回true

3

1.1.2-beta5,读取03版excel使用new BufferedInputStream(new FileInputStream("/xxxx/xxx/77.xls")读取依然会java.lang.NoClassDefFoundError: org/apache/poi/poifs/filesystem/FileMagic

9

如果是jdk>=1.8的话可以替换成poi4.0.0即可解决,有可能是poi的bug导致的

  • <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>

    换成4.0.0版本解决不了问题啊