读取 Microsoft Word 文档时出现 Android Apache POI 错误 : org. apache.xmlbeans.SchemaTypeLoaderException Cannot resolve type for handle

标签 android kotlin ms-word apache-poi docx

我正在尝试在我的 Android 应用程序中使用 Apache POI,以便可以读取 Microsoft Word 文件。我正在集成 POIA 库,它可以在 Android (https://github.com/SUPERCILEX/poi-android) 上使用 Apache POI。但是,当我运行代码并读取 Word 文件时,我收到以下错误:

Exception: org.apache.xmlbeans.SchemaTypeLoaderException: Cannot resolve type for handle _XY_Q=space|R=space

下面是我的代码(我在第一行收到错误):

var document = XWPFDocument(uri?.let { contentResolver.openInputStream(it) });
var extractor = XWPFWordExtractor(document);
var documentText = extractor.text; // Retrieve the document's text

在运行时我有以下代码:

super.onCreate(savedInstanceState)
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")

以下是我的 gradle(项目级):

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.3.60'
    repositories {
        google()
        jcenter()

    }
    ext {
        poiVersion = '3.17'
    }
    dependencies {
        classpath 'com.android.tools:r8:1.4.93'
        classpath 'com.android.tools.build:gradle:3.4.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://jitpack.io' }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

以下是我的 gradle(应用级):

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.example.diffchecker"
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    // For developers using the Android Support Library
    implementation 'pub.devrel:easypermissions:2.0.1'
    implementation 'com.google.android.gms:play-services-ads:18.3.0'
    implementation ("com.github.SUPERCILEX.poi-android:poi:$poiVersion") {
        exclude group: 'stax', module: 'stax-api'
    }
}

下面是完整的堆栈跟踪:

I/System.out: Exception: org.apache.xmlbeans.SchemaTypeLoaderException: Cannot resolve type for handle _XY_Q=space|R=space@http://www.w3.org/XML/1998/namespace (schemaorg_apache_xmlbeans.system.sF1327CCA741569E70F9CA8C9AF9B44B2.cttext7f5btype) - code 13

重要提示:我正在尝试读取用户选择的 Word 文件的内容,而不是来自硬编码路径的 Word 文件。

请在您的答案中包含所有相关代码,并且您的代码应该适用于 Android API 22

最佳答案

这里的库所有者。 poi-android 仅使用 Excel 文件进行了测试,并且经过量身定制,可与 proguard 很好地配合,以最小化我编写的应用程序的二进制大小。

如果我不得不猜测,this导致您的问题。对于更通用的 POI 版本,请使用 https://github.com/centic9/poi-on-android (这是我的库所基于的)。您必须包含 releases section 中的 JAR。进入你的源代码树并添加这个 Gradle 依赖项:implementation files('path/to/file.jar')。但是,您会注意到,该库非常大(13MB+),因此修改 POI 以适应我的用例的原因。我建议 fork 其中一个库并根据您的用例对其进行定制。

关于读取 Microsoft Word 文档时出现 Android Apache POI 错误 : org. apache.xmlbeans.SchemaTypeLoaderException Cannot resolve type for handle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60134114/

相关文章:

android - Android 12 中的异常

java - 如何在kotlin中使用lambda进行匿名内部类替换

c# - 简化/清理 DOCX word 文档的 XML

javascript - 为什么使用 API 上传 Word OfficeJS PDF 文档会保存一个空的 PDF 文档

c# - 将一个文档插入另一个 Microsoft.Office.Interop.Word

java - (应用程序崩溃)返回空指针异常...当从 Serilizable 类调用公共(public)类的方法时

android ble连接慢

android - 我的错误 "unexpected element <queries> found in <manifest>",如何解决?

android - 无法访问类 'android.support.design.widget.Snackbar'

spring - 如何在Prometheus中公开webClient指标?