我分别使用了里面的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.
几个构造方法: ` // 可以完成 @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);
}`