前言:
前一段时间,帮女朋友整理她们公司的破Excel文档,本着减少工作量的原则(居家好男人),帮忙写了个java main去读取整理Excel,到后来发现在读取到xlsx的excel报错,报错信息居然没看懂。。。
报错信息
Exception in thread "main" cn.hutool.poi.exceptions.POIException: IOException: OPC Compliance error [M4.3]: Producers shall not create a document element that contains refinements to the Dublin Core elements, except for the two specified in the schema: <dcterms:created> and <dcterms:modified> Consumers shall consider a document element that violates this constraint to be an error.
at cn.hutool.poi.excel.WorkbookUtil.createBook(WorkbookUtil.java:87)
at cn.hutool.poi.excel.WorkbookUtil.createBook(WorkbookUtil.java:48)
at cn.hutool.poi.excel.ExcelReader.<init>(ExcelReader.java:64)
at cn.hutool.poi.excel.ExcelReader.<init>(ExcelReader.java:54)
at cn.hutool.poi.excel.ExcelUtil.getReader(ExcelUtil.java:217)
at cn.hutool.poi.excel.ExcelUtil.getReader(ExcelUtil.java:193)
at com.ls.handler.option_excel.ExcelReaderExcel.readExcel(ExcelReaderExcel.java:53)
at com.ls.handler.option_excel.ExcelReaderExcel.main(ExcelReaderExcel.java:123)
Caused by: java.io.IOException: OPC Compliance error [M4.3]: Producers shall not create a document element that contains refinements to the Dublin Core elements, except for the two specified in the schema: <dcterms:created> and <dcterms:modified> Consumers shall consider a document element that violates this constraint to be an error.
at org.apache.poi.ss.usermodel.WorkbookFactory.createWorkbook(WorkbookFactory.java:351)
at org.apache.poi.ss.usermodel.WorkbookFactory.createXSSFWorkbook(WorkbookFactory.java:314)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:302)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:271)
at cn.hutool.poi.excel.WorkbookUtil.createBook(WorkbookUtil.java:85)
... 7 more
Caused by: org.apache.poi.openxml4j.exceptions.InvalidFormatException: OPC Compliance error [M4.3]: Producers shall not create a document element that contains refinements to the Dublin Core elements, except for the two specified in the schema: <dcterms:created> and <dcterms:modified> Consumers shall consider a document element that violates this constraint to be an error.
at org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.checkElementForOPCCompliance(PackagePropertiesUnmarshaller.java:259)
at org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.checkElementForOPCCompliance(PackagePropertiesUnmarshaller.java:291)
at org.apache.poi.openxml4j.opc.internal.unmarshallers.PackagePropertiesUnmarshaller.unmarshall(PackagePropertiesUnmarshaller.java:113)
at org.apache.poi.openxml4j.opc.OPCPackage.getParts(OPCPackage.java:741)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:274)
at org.apache.poi.xssf.usermodel.XSSFWorkbookFactory.createWorkbook(XSSFWorkbookFactory.java:115)
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.apache.poi.ss.usermodel.WorkbookFactory.createWorkbook(WorkbookFactory.java:339)
... 11 more
实现逻辑
我的实现是,把所有的excel文件都放到一个文件夹里,去读取,里边有xls文件也有xlsx文件,xls文件是没问题的,但是xlsx文件会报这个错。
异常原因和解决
为了发现异常,我再执行文件解析的时候,打印了一下文件名,发现一个问题
可以清楚的看到“~$蔺一铭.xlsx”,读取的并不是一个正常的文件,这是一个类似于临时文件,而且是隐藏文件,以为找到了原因了,把contains ~的文件排除掉不就行了。
后续
到这里我以为终于解决了这个问题,但是我后来轮循的时候发现,又出了这个异常!整个人有点崩溃,仔细排查了文件名,完全没问题,也不是临时文件,抱着试一试的把有问题的xlsx文件打开随便编辑一下再保存,重新读取,居然异常没有了…
我只能说,POI虽然方便了我们读取、写入EXCEL,但是架不住跟EXCEL的兼容性还是不行,莫名其妙的出各种问题,希望以后可以更加强大吧…