[英]Exception in thread “main” org.apache.poi.POIXMLException exception appearing while reading data from xlsx file using apache POI
Please find below the code..... 请在下面的代码中找到.....
package sanityTests;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
public class ApachePOI {
public static void main(String[] args) throws Exception {
File src = new File("C:/Users/Ishan/Desktop/TestData.xlsx");
FileInputStream fis = new FileInputStream(src);
XSSFWorkbook wb = new XSSFWorkbook(fis); // To deal with XLSX workbook
//HSSFWorkbook wb1 = new HSSFWorkbook(fis1); // To deal with XLS workbook
XSSFSheet sheet1 =wb.getSheetAt(0);
String data0 = sheet1.getRow(0).getCell(0).getStringCellValue();
System.out.println(data0);
wb.close();
}
}
While running the program I am getting the following issue:- 运行该程序时,出现以下问题:
Exception in thread "main" org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:65)
at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:601)
at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:174)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:279)
at sanityTests.ApachePOI.main(ApachePOI.java:20)
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:56)
at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:62)
... 4 more
Caused by: java.lang.NoSuchMethodError: org.apache.xmlbeans.XmlOptions.setLoadEntityBytesLimit(I)Lorg/apache/xmlbeans/XmlOptions;
at org.apache.poi.POIXMLTypeLoader.<clinit>(POIXMLTypeLoader.java:50)
at org.apache.poi.xssf.model.ThemesTable.<init>(ThemesTable.java:85)
... 10 more
If I change the format of the file from xlsx to xls it works fine.Not sure how to resolve the error. 如果我将文件格式从xlsx更改为xls,则可以正常工作。不确定如何解决该错误。 I have included all the jars like xml-beans and poi jars. 我已经包括了所有的xml-beans和poi罐子。 Still getting the error. 仍然出现错误。 Please help 请帮忙
You haven't provided any version numbers for any of your components. 您尚未为任何组件提供任何版本号。 However, XmlOptions.setLoadEntityBytesLimit(int)
was only added in XMLBeans version 2.6.0 (from August 2012), and will not be present in 2.4.0. 但是, XmlOptions.setLoadEntityBytesLimit(int)
仅在XMLBeans 版本2.6.0 (自2012年8月起)中添加,在2.4.0中不存在。
As far as I can tell, POI has used 2.6.0 itself for quite a long time, but somehow an older version of the library has been included in your runtime dependencies, and will need updating. 据我所知,POI长时间使用2.6.0本身,但是以某种方式,库的旧版本已包含在运行时依赖项中,并且需要更新。
To sum up: include XMLBeans 2.6.0 in your project and POI will work fine. 总结:在您的项目中包含XMLBeans 2.6.0,POI可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.