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 文件
如果直接按照官網(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 包
放入這個(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")
}
}
}