读取Microsoft Word文档时出现Android Apache POI错误:org.apache.xmlbeans.SchemaTypeLoaderException无法解析句柄的类型 [英] Android Apache POI error when reading Microsoft Word document: org.apache.xmlbeans.SchemaTypeLoaderException Cannot resolve type for handle

查看:1428
本文介绍了读取Microsoft Word文档时出现Android Apache POI错误:org.apache.xmlbeans.SchemaTypeLoaderException无法解析句柄的类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use Apache POI in my Android application so that I can read Microsoft Word files. I am integrating the POIA library which enables Apache POI usage on Android (https://github.com/SUPERCILEX/poi-android). However, when I run my code and read in a Word file, I receive the following error:

异常:org.apache.xmlbeans.SchemaTypeLoaderException:无法 解析句柄_XY_Q = space | R = space

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

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

Below is my code (I am receiving error on the first line):

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

在运行时,我有以下代码:

At runtime I have the following code:

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")

下面是我的毕业证书(项目级别):

Below is my gradle (project-level):

// 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(应用程序级):

Below is my gradle (app-level):

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'
    }
}

下面是完整的堆栈跟踪:

Below is the full stack trace:

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上运行.

Please include ALL the relevant code in your answer, and your code should work on Android API 22.

推荐答案

此处的库所有者. poi-android仅使用Excel文件进​​行了测试,并且经过精心设计以与proguard配合使用,以最小化我编写的应用程序的二进制大小.

owner of the lib here. poi-android has only been tested with Excel files and is tailored to work well with proguard to minimize binary size for an app I wrote.

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

If I had to guess, this is causing your issue. For a more generic version of POI, use https://github.com/centic9/poi-on-android (which is what my lib is based off of). You'll have to include the JAR from the releases section into your source tree and add this Gradle dependency: implementation files('path/to/file.jar'). However, as you'll notice, the lib is quite large (13MB+), hence the reasoning behind modifying POI to my use case. I would recommend forking one of the libs and tailoring it to your use case.

这篇关于读取Microsoft Word文档时出现Android Apache POI错误:org.apache.xmlbeans.SchemaTypeLoaderException无法解析句柄的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆