android 使用 apache poi 读取 xlsx 文件时出现错误停止,如何解决

android read xlsx file with apache poi stop with error , how to solve

我正在使用 apache.poi 3.15 从 android 读取 xlsx 文件。我的应用程序在 2 种情况下显示错误 条件一: 如果我 运行 我的应用程序 没有 xml-beans.jar ,应用程序将被构建并且 运行 我可以清除

XSSFWorkbook workbook = null;

所以不会出现错误,但是如果我将代码更改为这个

InputStream fIn = cntx.getResources().getAssets().open("a.xlsx", cntx.MODE_WORLD_READABLE);
XSSFWorkbook workbook = null;
workbook=new XSSFWorkbook(fIn);

应用程序将成功构建,在 运行 之后将停止并显示此错误:

java.lang.VerifyError: org/apache/poi/xssf/usermodel/XSSFWorkbook

条件二:如果我依赖xmlbeans.jar,android工作室将终止构建过程并显示此错误:

Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'. > com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/apache/xmlbeans/xml/stream/Location.class

这是我的 gradle :

    defaultConfig {
    applicationId "com.ramin.test"
    minSdkVersion 17
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    //enable Multi Dex
   multiDexEnabled true

}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

dexOptions {
    preDexLibraries false;
    javaMaxHeapSize "4g"
}
afterEvaluate {
    tasks.matching {
        it.name.startsWith('dex')
    }.each { dx ->
        if (dx.additionalParameters == null) {
            dx.additionalParameters = ['--multi-dex']
        } else {
            dx.additionalParameters += '--multi-dex'
        }
    }
}

dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:cardview-v7:23.4.0'
compile 'com.android.support:recyclerview-v7:23.4.0'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support:design:23.1.1'
compile files('libs/poi-3.15.jar')
compile files('libs/poi-ooxml-schemas-3.15.jar')
compile files('libs/poi-ooxml-3.15.jar')
compile files('libs/xmlbeans-2.6.0.jar')}

我该怎么做

终于我在这里找到了解决方案https://github.com/andruhon/AndroidReadXLSX .只需要附加这个 jar 文件就可以了! :

compile files('libs/aa-poi-3.10-min-0.1.5.jar')
compile files('libs/aa-poi-ooxml-schemas-3.10-reduced-more-0.1.5.jar')

和 gradle 中我们需要构建的其他行:

    defaultConfig {
   multiDexEnabled true

}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
dexOptions {
    preDexLibraries false;
    javaMaxHeapSize "4g"
}

afterEvaluate {
    tasks.matching {
        it.name.startsWith('dex')
    }.each { dx ->
        if (dx.additionalParameters == null) {
            dx.additionalParameters = ['--multi-dex']
        } else {
            dx.additionalParameters += '--multi-dex'
        }
    }
}

也许这对某些人有帮助...