我能够在基于OSGI的应用程序中正确读取XLS文件,但是当我尝试读取XLSX文件时,我收到以下错误。
Caused by: java.lang.ExceptionInInitializerError
at org.apache.poi.openxml4j.opc.OPCPackage.init(OPCPackage.java:162)
at org.apache.poi.openxml4j.opc.OPCPackage.<init>(OPCPackage.java:142)
at org.apache.poi.openxml4j.opc.Package.<init>(Package.java:37)
at org.apache.poi.openxml4j.opc.ZipPackage.<init>(ZipPackage.java:128)
at org.apache.poi.openxml4j.opc.OPCPackage.open(OPCPackage.java:257)
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:291)[242:export_poi.jar:0.0.0]
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:252)[242:export_poi.jar:0.0.0]
at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:231)[242:export_poi.jar:0.0.0]
at mycom.project2.ExcelAdapter.Sources.Source.readExcelFile(Source.java:67)[232:ExcelAdapter:1.0.0]
at mycom.project2.ExcelAdapter.Sources.Source.<init>(Source.java:56)[232:ExcelAdapter:1.0.0]
at mycom.project2.ExcelAdapter.Sources.SourcesManager.addSource(SourcesManager.java:55)[232:ExcelAdapter:1.0.0]
at mycom.project2.ExcelAdapter.Sources.SourcesManager.addSources(SourcesManager.java:48)[232:ExcelAdapter:1.0.0]
at mycom.project2.ExcelAdapter.Processors.Engine.setResourceConfig(Engine.java:44)[232:ExcelAdapter:1.0.0]
at mycom.project2.ExcelAdapter.ExcelAdapter.configureBusinessLogic(ExcelAdapter.java:164)[232:ExcelAdapter:1.0.0]
at mycom.project1.function_engine_tooling.fb.libraries.FunctionBlockType.addFunctionBlockInstance(FunctionBlockType.java:161)[234:fb-libraries:1.0.0]
at mycom.project1.function_engine_tooling.fb.libraries.FunctionBlockType.handleCreateFbInstances(FunctionBlockType.java:373)[234:fb-libraries:1.0.0]
at mycom.project1.function_engine_tooling.fb.libraries.FunctionBlockType.onMessageReceived(FunctionBlockType.java:197)[234:fb-libraries:1.0.0]
at mycom.project2.ExcelAdapter.ExcelAdapterInstanceFactory.onMessageReceived(ExcelAdapterInstanceFactory.java:46)[232:ExcelAdapter:1.0.0]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)[:1.8.0_131]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)[:1.8.0_131]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)[:1.8.0_131]
at java.lang.reflect.Method.invoke(Method.java:498)[:1.8.0_131]
at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:408)
at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:279)
at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:252)
... 19 more
Caused by: java.lang.ClassCastException: org.apache.xerces.stax.XMLEventFactoryImpl cannot be cast to javax.xml.stream.XMLEventFactory
我正在使用此命令读取文件:
WorkbookFactory.create(new File("fileName"));
我在网上尝试了各种解决方案,它告诉我要排除某些artifactID,例如:stax-api和stax。但是没有一个解决方案似乎有效。
答案 0 :(得分:3)
问题似乎是有两个包导出包javax.xml.stream
。似乎org.apache.xerces.stax.XMLEventFactoryImpl连接到其中一个软件包,而您的软件包连接到另一个软件包。因此,即使它们的名称相同,他们也看不到相同的类。
为避免这种情况,请确保只有一个捆绑包导出此包。实现这一目标的一种方法是export the package from the system bundle因为它应该存在于jre中。
至少对于apache karaf来说,安装很简单。使用这些包:
install -s mvn:commons-codec/commons-codec/1.10
install -s mvn:org.apache.commons/commons-collections4/4.1
install -s mvn:org.apache.servicemix.bundles/org.apache.servicemix.bundles.poi/3.16_1
答案 1 :(得分:0)
我使用此代码打开XLSX文件没有问题:
InputStream inp = new FileInputStream(inputfilename);
Workbook wb = WorkbookFactory.create(inp);
// Open the specified sheet
Sheet inputsheet = wb.getSheetAt(sheetindex);
我不知道输入流是否必要,但它肯定能完成这项工作。