0

我正在尝试阅读 .xls、.xlsm 和 .xlsx 表。如果尝试读取 .xls 表,代码可以正常工作,但如果尝试读取 .xlsm 和 .xlsx 表则显示异常。我已经在我的类路径中配置了必要的 jars 文件-

poi-3.8-20120326.jar,
poi-examples-3.8-20120326.jar,
poi-excelant-3.8-20120326.jar,
poi-ooxml-3.8-20120326.jar,
poi-ooxml-schemas-3.8-20120326.jar,
poi-scratchpad-3.8-20120326.jar,
ooxml-schemas-1.0.jar,
xmlbeans-2.3.0.jar,
dom4j-1.6.1.jar,
geronimo-stax-api_1.0_spec-1.0.1.jar

但后来我也得到了错误-

Exception in thread "main" org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62)
at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:403)
at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:408)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:155)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:183)
at screens.LireEcrire.main(LireEcrire.java:26)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60)
... 5 more
Caused by: org.apache.xmlbeans.XmlException: error: duplicate attribute 'o:relid'
at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3471)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1270)
at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1257)
at   org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345)
at org.apache.xmlbeans.XmlObject$Factory.parse(XmlObject.java:663)
at org.apache.poi.xssf.usermodel.XSSFVMLDrawing.read(XSSFVMLDrawing.java:107)
at org.apache.poi.xssf.usermodel.XSSFVMLDrawing.<init>(XSSFVMLDrawing.java:102)
... 10 more
Caused by: org.xml.sax.SAXParseException: duplicate attribute 'o:relid'
at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.reportFatalError(Piccolo.java:1038)
at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.parse(Piccolo.java:723)
at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3439)
... 16 more
4

2 回答 2

1

Excel 生成错误的 xml。可能是特别的。

阅读:https ://bz.apache.org/bugzilla/show_bug.cgi?id=53819

它是由错误的 xml 引起的。

这可以通过手动删除属性的重复定义来解决。

堆栈跟踪告诉该文件包含无效的 xml。我检查了附件,确实如此: /xl/drawings/vmlDrawing1.vml 包含不允许的重复属性,请查看以下片段:

  <v:fill o:relid="rId1" o:relid="rId1" o:relid="rId1" o:relid="rId1" o:relid="rId1"
   o:relid="rId1" o:relid="rId1" o:relid="rId1" o:relid="rId1" o:relid="rId1"
   o:relid="rId1" o:relid="rId1" o:relid="rId1" o:relid="rId1" o:relid="rId1"
   o:relid="rId1" o:relid="rId1" o:relid="rId1" o:relid="rId1" o:relid="rId1"
   o:relid="rId1" o:relid="rId1" o:relid="rId1" o:relid="rId1" o:relid="rId1"

您可以将 xslx 作为 zip 存档打开,找到此文件并修复它。

于 2016-09-30T20:10:35.207 回答
0

您可能需要POI-XSSF来读取新格式的 XLS 文件(亲切地命名为 .XLSX)。

于 2012-05-24T13:25:52.280 回答