Android uses Poi to modify Doc documents

Table of contents

foreword

1. What is POI?

2. Use steps

1. Import library

2. Create Poi tool class

3. Quote

3. Template style

Summarize



foreword

 Fill in or select on the interface and then assign the selected content to the word document. Documents are templates.

Different versions of Android need to adapt to read files.


 

1. What is POI?

 POI is an open source library of the Apache Software Foundation. POI provides an API for Java programs to read and write files in Microsoft Office format.

structure:

HSSF - Provides the ability to read and write files in Microsoft Excel format.

XSSF - Provides the ability to read and write files in Microsoft Excel OOXML format.

HWPF - Provides the ability to read and write files in Microsoft Word format.

HSLF - Provides the ability to read and write files in Microsoft PowerPoint format.

HDGF - Provides the ability to read and write files in Microsoft Visio format. 

2. Use steps

1. Import library

  Poi download address

The project introduces the following three jar packages, I am using version 3.9

 

 Poi download address

 
 

 

2. Create Poi tool class

 


object PoiUtil {
    private val TAG = "PoiUtil"

    /**
     * 直接读取asset目录下的模板文件
     * @param context 上下文
     * @param openFileName 模板的文件名称
     * @param savePath 保存得文件路径
     * @param saveName 保存得文件名
     * @param map 需要替换的内容
     *
     * @return
     */
    fun init(
        context: Context,
        openFileName: String,
        savePath: String,
        saveName: String,
        map: HashMap<String, String>
    ) = if (writeToDoc(context.assets.open(openFileName), savePath, saveName, map)) {
        "$savePath$saveName"
    } else {
        null
    }


    /**
     * @param templetDocInStream 模板文件的InputStream
     * @param targetDocPath 保存得文件路径
     * @param targetDocName 保存得文件名
     * @param dataMap 需要替换的内容
     *
     * @return 成功 true 失败 false
     */
    private fun writeToDoc(
        templetDocInStream: InputStream,
        targetDocPath: String,
        targetDocName: String,
        dataMap: Map<String, String>
    ): Boolean {
        var file = File(targetDocPath)
        if (!file.exists()) {
            file.mkdirs()
        }
        file = File(targetDocPath + targetDocName)
        if (!file.exists()) {
            file.createNewFile()
        }

        val doc = HWPFDocument(templetDocInStream) //获取word文本内容,整个文本
        val out = FileOutputStream(file) //把doc输出到输出流中HDoc.write(out);
        try { //得到模板doc文件的HWPFDocument对象
            val range = doc.range //替换文本内容,将自定义的$xxx$替换成实际文本
            dataMap.entries.forEach {
                range.replaceText(it.key, it.value)
            }
            //写到另一个文件中
            doc.write(out)
            Log.e(TAG, "writeToDoc  成功保存路径》》${targetDocPath}$targetDocName")
            out.close()

            templetDocInStream.close()
            return true
        } catch (e: IOException) {
            Log.e(TAG, "writeToDoc IOException: $e")
            e.printStackTrace()
            return false
        } catch (e: Exception) {
            Log.e(TAG, "writeToDoc Exception: $e")
            e.printStackTrace()
            return false
        } finally {
            out.close()
            templetDocInStream.close()
        }

    }

}

3. Quote

        val m = HashMap<String, String>()
// %xxxx% 是要和模板定义的标识一致
            m["%add%"] = "青岛"
            m["%preson%"] = "李四"
            m["%idcard%"] = "130109696945655"
            m["%year%"] = "2022" 
            PoiUtil.init(this, "doc/外出请假申请书.doc", "/sdcard/docs/", "外出请假申请书2.doc", m)

3. Template style

Everyone's template is different


 

Summarize

 Different versions of Android need to adapt to read files.

The Poi I am using is version 3.9.

Guess you like

Origin blog.csdn.net/weixin_41620505/article/details/127088278