我正在尝试使用以下代码实例化Workbook
:
Workbook workbook = WorkbookFactory.create(new File(excelWorkbookFilePath));
我是这样做的,因为工作簿可以是.xlsx
或.xls
。但是,它最终会抛出这个错误:
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:249)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:293)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:252)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:231)
at com.iconectiv.handle.ExcelHandle.<init>(ExcelHandle.java:43)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:56)
at org.apache.poi.POIXMLFactory.createDocumentPart(POIXMLFactory.java:62)
... 9 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)
... 15 more
我做了一些研究,发现InvocationTargetException
不是真正的例外,所以我尝试用catch
语句来抓住它:
} catch (InvocationTargetException e) {
e.getCause().printStackTrace();
}
但是,我在Eclipse中遇到一个错误,说我无法捕捉到它,因为它永远不会被抛出。为什么我收到此错误?任何帮助将不胜感激,提前谢谢!
注意:我使用的是Apache POI 3.14。
答案 0 :(得分:5)
抛出的实际错误是
NoSuchMethodError: org.apache.xmlbeans.XmlOptions.setLoadEntityBytesLimit(I)Lorg/apache/xmlbeans/XmlOptions;
您可能在类路径中没有xmlbean jar,或者您可能有多个版本的jar
答案 1 :(得分:1)
您可以根据文件名单独为.xls和.xlsx扩展名实例化工作簿:
Workbook wb = null;
if (fileName.endsWith(".xlsx")) {
wb = new XSSFWorkbook(in);
}else{
fs = new POIFSFileSystem(in);
wb = (Workbook) new HSSFWorkbook(fs);
}
答案 2 :(得分:0)
我用相同的版本更新了pom。早些时候,我对poi-ooxml使用的版本是3.9.0,对于poi是4.0。
<ContentPage.Content>
<StackLayout>
<WebView
x:Name="website"
HeightRequest="1000"
Navigated="Website_Navigated"
Navigating="Website_Navigating"
WidthRequest="1000" />
</StackLayout>
</ContentPage.Content>
public partial class Page19 : ContentPage
{
public Page19 ()
{
InitializeComponent ();
UrlWebViewSource source = new UrlWebViewSource();
//source.Url = "https://www.google.com";
source.Url = "http://www.pdf995.com/samples/pdf.pdf";
website.Source = source;
}
private void Website_Navigated(object sender, WebNavigatedEventArgs e)
{
website.IsVisible = false;
}
private void Website_Navigating(object sender, WebNavigatingEventArgs e)
{
website.IsVisible = true;
if (e.Url.Contains("pdf") && !e.Url.Contains("drive.google.com"))
{
var webview = (WebView)sender;
website.Source = "https://drive.google.com/viewerng/viewer?embedded=true&url=" + e.Url;
}
}
}
对我有用