word 模板替換 poi tl簡單使用

2021-10-17 04:16:16 字數 3085 閱讀 2372

工作中,需要給word 中的一些標籤替換為 指定的資料;暫時來記錄一下,並且網上找了一些 ,經典的  poi, itext 等,順便找了幾個比較好用的 嘗試使用了下並記錄下來
poi-tl

簡介: 輕量, 主要針對word的模板引擎,提供了對word的豐富操作。

官網 其實官網很詳細了,自己就照搬一下吧。

pom

com.deepoove<

/groupid>

poi-tl<

/artifactid>

1.9.1

<

/version>

<

/dependency>

**示例

基本使用

// sourcedoc 模板檔案

//obj模板需要替換的資料,map or 乙個物件;物件的屬性和 模板的標籤相對應即可

xwpftemplate.

compile

(sourcedoc)

.render

(object obj/*可以是乙個map 也可以是乙個物件*/).

writetofile

(targetdoc)

;

// 構建資料

// 文字型別

textrenderdata a2 = texts.of(

"o")

.style

(style.

builder()

.buildfontfamily

("wingdings").

buildfontsize(28

).build()

).create()

;// 型別

;// **型別

tablerenderdata a4 = tables.

create

( rows.of(

"標題"

,"備註").

textcolor

("ffffff").

textfontfamily

("微軟雅黑").

textfontsize(16

).center()

.bgcolor

("4472c4").

create()

, rows.

create

("poi-tl"

,"介紹"))

;// 列表型別

numberingrenderdata a5 = numberings.of(

"周瑜"

,"孔明").

create()

;

date a6 =

newdate()

;

templatedata =

newhashmap

()};

poitlentity =

newpoitlentity

("title"

,"a1"

, a2, a3, a4, a5, a6)

;// 使用map構建資料

xwpftemplate.

compile

(sourcedoc)

.render

(templatedata)

.writetofile

("newtest01.docx");

// 使用物件構建資料

xwpftemplate.

compile

(sourcedoc)

.render

(poitlentity)

.writetofile

("newtest01.docx"

);

模板

效果

spring spel 表示式配置使用

org.springframework<

/groupid>

spring-expression<

/artifactid>

4.3.6

.release<

/version>

<

/dependency>

//配置使用 spring spel表示式

configure configure = configure.

builder()

.usespringel()

.build()

;// 使用map構建資料

xwpftemplate.

compile

(sourcedoc, configure)

.render

(templatedata)

.writetofile

("newtest01.docx");

// 使用物件構建資料

xwpftemplate.

compile

(sourcedoc, configure)

.render

(poitlentity)

.writetofile

("newtest01.docx"

);

以上簡單入門 很容易上手,另外還有其他的比較好的api操作,官網api檢視

匯出替換Word模板中的內容

if filepath.tostring tolowercase endswith docx runtext oneparastring for map.entryentry hmocrgkeytovalue.entryset else 網上有很多先移除runs在新增,這種做法模板裡面的樣式會消失,...

關於模板引擎poi tl的使用

1.介紹 poi tl的使用文件位址是 2.使用 這邊只說明下自己用到的標籤及功能其他功能需要的可以參考官方文件 2.1物件屬性填值 模板是這樣 對應的後端 2.2 模板 大括號是傳入的物件中括號是屬性填充 一行的物件 使用乙個list裝入 有多少行裝多少個poitl的 物件 2.3統計圖 統計圖的...

Csharp 簡單操作Word模板檔案

1.建立乙個模板的word文件 doc1.dot 內容為 to sub website is ok geovindu 塗聚文好樣的 work.ceothanks.2.程式操作 為 簡單操作word模板檔案 塗聚文 2011 11 03 締友計算機資訊技術 private void button2 c...