Unable to read XLSX file with Apache POI 3.14

I can read XLS files correctly in my OSGI based application, but when I try to read the XLSX file I get the following error.

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

      

I am using this command to read files:

WorkbookFactory.create(new File("fileName"));  

      

I have tried various solutions on the internet that tell me to exclude some artifacts like: stax-api and stax. But none of the solutions seem to work.

+3


source to share


2 answers


The problem is that there are two packages that export a package javax.xml.stream

. It looks like org.apache.xerces.stax.XMLEventFactoryImpl is connected to one of these packages and your package is connected to the other. Thus, they do not see the same classes, even if they are called the same.

To avoid this, make sure that only one package exports this package. One way to achieve this is to export the package from the system package as it must be present in jre.



At least for apache karaf, the installation is straightforward. Use these packages:

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

      

+3


source


I open XLSX files with this code without issue:

    InputStream inp = new FileInputStream(inputfilename);
    Workbook wb = WorkbookFactory.create(inp);
    // Open the specified sheet
    Sheet inputsheet = wb.getSheetAt(sheetindex);

      



I don't know if an input stream is needed, but it definitely does the job.

0


source







All Articles