Android 使用 poi 或 poi-tl 生成 world 文件

Android 使用 Poi 生成 World 文件

注意,查看了 poi-tl 的 release,它在 1.5.0 開(kāi)始,升級(jí)了 poi 組件到 4.0.0,而 poishadow-all.jar 這個(gè)兼容Android的 jar 只支持到了 POI 3.17 ,所以如果導(dǎo)入 poi-tl 1.5.0 以上版本會(huì)有兼容問(wèn)題,導(dǎo)致無(wú)法使用。如果確實(shí)需要使用 poi-tl ,建議使用 1.4.0 版本,例如我當(dāng)初接入的時(shí)候使用的是 1.4.2


在 Android 平臺(tái)中,如果直接導(dǎo)入并使用 poi 會(huì)直接無(wú)法編譯

如果要使用原生的 poi,推薦看看這個(gè) issues

作者編譯了一個(gè) poishadow-all.jar 庫(kù),直接導(dǎo)入這個(gè) jar 就可以使用了
這是 作者倉(cāng)庫(kù)

但是我想使用 poi-tl ,因?yàn)樗梢灾苯邮褂媚0逄畛洌耆珴M足我目前的工作需求。

Android 使用 poi-tl 填充生成 world 文件

poi-tl 官網(wǎng)

使用 poi-tl 填充 world 入門

如果直接按照官網(wǎng)的操作,使用 gradle 導(dǎo)入

compile group: 'com.deepoove', name: 'poi-tl', version: '1.4.2'

這樣是無(wú)法編譯的。解決方法

采用這樣的方式導(dǎo)入,在 build.gradle

implementation('com.deepoove:poi-tl:1.4.2') {
        //在Android 中 poi-tl 所依賴的庫(kù)會(huì)導(dǎo)致無(wú)法編譯
        //所以只使用 poi-tl 的填充功能
        //然后使用其他作者提供的 poi
        //下面是過(guò)濾掉 poi-tl 的依賴庫(kù)
        exclude group: 'org.apache.xmlbeans', module: 'xmlbeans'
        exclude group: 'org.apache.poi', module: 'poi-ooxml'
        exclude group: 'org.apache.poi', module: 'poi-ooxml-schemas'
        exclude group: 'org.apache.poi', module: 'poi'
    }

主要是下面的 exclude ,把poi-tl 的依賴全部取消掉,然后到這個(gè)倉(cāng)庫(kù)下載可用的 jar

也可以 點(diǎn)擊直接下載 jar 包

放入這個(gè) jar 包到app工程的 libs 中,然后在 build.gradle 文件中

    implementation files('libs/poishadow-all.jar')

至此已經(jīng)添加好了依賴庫(kù),還有最后一步操作,執(zhí)行下面的操作

(你可以在 onCreate 前,或者初始化的時(shí)候執(zhí)行它,但必須保證是在使用 poi 前)

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

就可以了

其它使用方式就和 poi-tl 沒(méi)有任何差別了

    /**
     * @param path 需要保存的world的路徑
     */
    fun generaWorld(path: String, map: HashMap<String, Any>?, mActivity: Context?) {
        LogUtil.v(TAG, "save world to file,map:" + LogUtil.objToString(map))

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

        val inputStream = mActivity?.assets?.open("model.docx")
        if (inputStream == null) {
            LogUtil.e("can not read template file,please check assets:model.docx")
            Toast.show(mActivity?.getString(R.string.save_faild_cannot_read_file))
            return
        }

        val template = XWPFTemplate.compile(inputStream)
        map?.let {
            template.render(map)
        }
        var outputStream: FileOutputStream? = null

        try {
            outputStream = FileUtil.instant.getFileOutputStream(path)
            if (outputStream == null) {
                LogUtil.w(TAG, "world path:$path is null,check permiss")
                Toast.show(mActivity.getString(R.string.save_faild_cannot_writ))
                return
            }
            template.write(outputStream)
            outputStream.flush()
            Toast.show("save success,path:$path")
        } catch (e: IOException) {
            LogUtil.e(TAG, "world write to output stream io exception:" +
                    LogUtil.objToString(e))
            Toast.show(mActivity.getString(R.string.save_faild_cannot_writ))
        } finally {
            try {
                template.close()
                outputStream?.close()
            } finally {
                LogUtil.i(TAG, "write world to $path output stream over")
            }
        }
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容