[alibaba/easyexcel]解析excel 用推荐的ExcelReader不传ExcelTypeEnum的方法 就无法识别他的类型

2024-06-20 748 views
0

` public static void noModelMultipleSheet() { InputStream inputStream=null; try { inputStream = new FileInputStream("C:\Users\xurong\Desktop\abc.xlsx");

        ExcelReader reader = new ExcelReader(inputStream,null,
                new AnalysisEventListener<List<String>>() {
                    @Override
                    public void invoke(List<String> object, AnalysisContext context) {
                        System.out.println(
                                "当前sheet:" + context.getCurrentSheet().getSheetNo() + " 当前行:" + context.getCurrentRowNum()
                                        + " data:" + object);
                    }
                    @Override
                    public void doAfterAllAnalysed(AnalysisContext context) {

                    }
                });

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

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

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.example.demo.DockerTestController.noModelMultipleSheet(DockerTestController.java:47) at com.example.demo.DockerTestController.main(DockerTestController.java:26) 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.(XlsxSaxAnalyser.java:44) at com.alibaba.excel.analysis.ExcelAnalyserImpl.getSaxAnalyser(ExcelAnalyserImpl.java:31) ... 4 more

回答

8

我也遇到同样的问题

8

使用1.1.1不传ExcelTypeEnum会报错,内容如下:

public static void main(String[] args) throws Exception{
        InputStream inputStream = getInputStream("d:/test.xlsx");
        try {
            // 解析每行结果在listener中处理
            MyExcelListener listener = new MyExcelListener();
            ExcelReader excelReader = new ExcelReader(inputStream,null, listener);
            excelReader.read();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

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.lib.easyexcel.service.ReadExcel.main(ReadExcel.java:19) 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.(XlsxSaxAnalyser.java:44) at com.alibaba.excel.analysis.ExcelAnalyserImpl.getSaxAnalyser(ExcelAnalyserImpl.java:31) ... 3 more

8

请问,问题解决了吗?

9

我使用POI4.0.0还会报错。下面有一个回答提供了一个解决方案:

public void read(String filePath,ExcelTypeEnum type,AnalysisEventListener<T> listener,
                     Class<T> model,boolean isTrim) throws IOException{
        File excelFile = new File(filePath);
        if(!excelFile.exists()){
            throw new NotFoundFileException("This file was not found, and the target path is:[ "+
                    filePath +" ]");
        }
        //使用BufferedInputStream装饰一下FileInputStream
        read(new BufferedInputStream(new FileInputStream(filePath)),type,listener,model,isTrim);
    }
2

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

9

2.2.10同样的问题

1

属实