加载 XLSX 文件时"Provider com.bea.xml.stream.EventFactory not found"



我已经在Java Desktop应用程序上成功使用了Apache POI,并希望在Android上使用它进行阅读&编写Excel文件。

这是我的github回购:https://github.com/anta40/stockchecker

每次我尝试打开XLSX文件时,该应用最终由于

而崩溃

org.apache.poi.javax.xml.Stream.fractoryConfigurationError:提供者 com.bea.xml.Stream.EventFactory找不到

这是我的build.gradle的内容: 应用插件:'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.anta40.app.stockchecker"
        minSdkVersion 15
        targetSdkVersion 28
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.github.SUPERCILEX.poi-android:poi:3.17'
    implementation 'com.github.angads25:filepicker:1.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

如何解决此问题?

在build.gradle上添加此行:

实现'com.fasterxml:aalto-xml:1.1.0'

不起作用。您会收到类似这样的错误消息:

重复的课程 org.codehaus.stax2.ri.typed.valuedecoderfactory $ intdecoder 模块POI-3.17.JAR(com.github.supercilex.poi-android:poi:3.17(和 stax2-api-4.1.jar(org.codehaus.woodstox:stax2-api:4.1(重复 类org.codehaus.stax2.ri.typed.valuedecoderfactory $ integerdecoder 在模块中发现POI-3.17.Jar (com.github.supercilex.poi-android:poi:3.17(和stax2-api-4.1.jar (org.codehaus.woodstox:stax2-api:4.1(重复类 org.codehaus.stax2.ri.typed.valuedecoderfactory $ longarraydecoder找到 在模块中POI-3.17.Jar(com.github.supercilex.poi-android:poi:3.17( and stax2-api-4.1.jar(org.codehaus.woodstox:stax2-api:4.1(重复 类org.codehaus.stax2.ri.typed.valuedecoderfactory $ longdecoder 在模块中发现POI-3.17.Jar (com.github.supercilex.poi-android:poi:3.17(和stax2-api-4.1.jar (org.codehaus.woodstox:stax2-api:4.1(重复类 org.codehaus.stax2.ri.typed.valuedecoderfactory $ qnamedecoder 模块POI-3.17.JAR(com.github.supercilex.poi-android:poi:3.17(和 stax2-api-4.1.jar(org.codehaus.woodstox:stax2-api:4.1(重复 类org.codehaus.stax2.ri.typed.valueencoderfactory在模块中找到 POI-3.17.JAR(com.github.supercilex.poi-android:poi:3.17(和 stax2-api4.1.jar(org.codehaus.woodstox:stax2-api:4.1(

exculde this stax2-api-4.1.jar在您的依赖性中并重建项目

编辑1-我知道我在编辑中有点晚,但是现在就解决了,只需移动您的系统即可在您初始化应用程序的静态块中移动。这将解决您的问题。

public class YourActivity extends AppCompatActivity {
      //scope......
      static {
            System.setProperty(
                    "org.apache.poi.javax.xml.stream.XMLInputFactory",
                    "com.fasterxml.aalto.stax.InputFactoryImpl"
            );
            System.setProperty(
                    "org.apache.poi.javax.xml.stream.XMLOutputFactory",
                    "com.fasterxml.aalto.stax.OutputFactoryImpl"
            );
            System.setProperty(
                    "org.apache.poi.javax.xml.stream.XMLEventFactory",
                    "com.fasterxml.aalto.stax.EventFactoryImpl"
            );
        }

只需在您的build.gradle模块中包含这些依赖项:

implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '3.17'
implementation group: 'org.apache.xmlbeans', name: 'xmlbeans', version: '3.1.0'
implementation 'javax.xml.stream:stax-api:1.0'
implementation 'com.fasterxml:aalto-xml:1.2.2'

不要使用依赖项的最新版本,因为它们会产生更多的错误。

感谢 animesh sharma 答案。

但是,您应该扩展应用程序类并在此处添加以避免重复任何代码。

class AppController : Application() {
    init {
        System.setProperty(
            "org.apache.poi.javax.xml.stream.XMLInputFactory",
            "com.fasterxml.aalto.stax.InputFactoryImpl"
        );
        System.setProperty(
            "org.apache.poi.javax.xml.stream.XMLOutputFactory",
            "com.fasterxml.aalto.stax.OutputFactoryImpl"
        );
        System.setProperty(
            "org.apache.poi.javax.xml.stream.XMLEventFactory",
            "com.fasterxml.aalto.stax.EventFactoryImpl"
        );
    }
}

也不要忘记将其添加到清单中。在块中添加 Android:name =&quort; AppController&quort&quort&quort&quot

最新更新