天天看點

poi-tl—一個超級好用開源的Word模闆引擎

1、poi-tl是什麼

poi-tl是一個基于Apache POI的Word模闆引擎,同時它也是一個免費開源(github位址)的Java類庫,給Java程式員帶來了word處理上的便捷。

2、官方介紹

在文檔的任何地方做任何事情(Do Anything Anywhere)是poi-tl的星辰大海。

2.1 poi-tl與其他word模闆引擎的對比

poi-tl—一個超級好用開源的Word模闆引擎
poi-tl—一個超級好用開源的Word模闆引擎

2.2.1 Template—模闆

模闆即Docx格式的Word文檔

2.2.2 Data-model—資料

資料即模闆中需要替換的資料結構,類似哈希或者字典,常用Map結構,其中key即需要替換的标簽

2.2.3 Output—輸出

輸出即最終文檔的流産生,可以是檔案流或網絡流等

3、軟體要求

Apache POI 4.1.2

jdk 1.8+

maven依賴

poi-tl—一個超級好用開源的Word模闆引擎

4、标簽

4.1 文本

标簽

{{var}}

資料模型

  1. String:文本
  2. TextRenderData:有樣式的文本
  3. HyperlinkTextRenderData :超連結和錨點文本
  4. Object:調用 toString() 方法轉化為文本

測試模闆

poi-tl—一個超級好用開源的Word模闆引擎
poi-tl—一個超級好用開源的Word模闆引擎
poi-tl—一個超級好用開源的Word模闆引擎

4.2 圖檔

{{@var}}

  1. String:圖檔url或者本地路徑。預設使用圖檔自身尺寸
  2. PictureRenderData
poi-tl—一個超級好用開源的Word模闆引擎

測試代碼

package com.lizba.poi;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureType;
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.data.Texts;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

/**
 * <p>
 *      圖檔測試
 * </p>
 *
 * @Author: Liziba
 * @Date: 2021/6/24 21:49
 */
public class PictureTest {

    public static void main(String[] args) throws IOException {
        String filePath = "D:\\poi-tl\\pictureTest.docx";
        String targetPath =  "D:\\poi-tl\\pictureTest2.docx";
        String picPath =  "D:\\poi-tl\\pic.jpg";
        XWPFTemplate template = XWPFTemplate.compile(filePath).render(
                new HashMap<String, Object>() {
                    {
                        // 方法一、圖檔路徑(原大小)
                        put("picture", picPath);
                        // 方法二、指定圖檔大小
                        put("picture", Pictures.ofLocal(picPath).size(420,350).center().create());
                        // 方法三、圖檔流
                        put("picture", Pictures.ofStream(new FileInputStream(picPath), PictureType.JPEG)
                                .size(420,350).create());
                        // 針對網絡圖檔、SVG圖檔、Java圖檔都有處理
                    }
                });
        template.writeAndClose(new FileOutputStream(targetPath));
    }

}
      
poi-tl—一個超級好用開源的Word模闆引擎

4.3 表格

{{#var}}

  1. TableRenderData
poi-tl—一個超級好用開源的Word模闆引擎
package com.lizba.poi;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.*;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

/**
 * <p>
 *      清單測試
 * </p>
 *
 * @Author: Liziba
 * @Date: 2021/6/24 21:49
 */
public class TableTest {

    public static void main(String[] args) throws IOException {
        String filePath = "D:\\poi-tl\\tableTest.docx";
        String targetPath =  "D:\\poi-tl\\tableTest2.docx";

        // 表頭
        RowRenderData tableHead = Rows.of("姓名", "性别", "位址", "微信公衆号").center().bgColor("4472c4").create();
        // 第一行
        RowRenderData row1 = Rows.create("張三", "男", "廣東深圳", "liziba_98");
        // 第二行
        RowRenderData row2 = Rows.create("李四", "男", "廣東深圳", "liziba_98");
        // 合并第一行和第二行的第二列與第三列
        MergeCellRule rule = MergeCellRule.builder().map(MergeCellRule.Grid.of(1, 1), MergeCellRule.Grid.of(2, 1))
                .map(MergeCellRule.Grid.of(1, 2), MergeCellRule.Grid.of(2, 2)).build();

        XWPFTemplate template = XWPFTemplate.compile(filePath).render(
            new HashMap<String, Object>() {
                {
                    put("table", Tables.of(tableHead, row1, row2).mergeRule(rule).center().create());
                }
            });
        template.writeAndClose(new FileOutputStream(targetPath));
    }

}
      
poi-tl—一個超級好用開源的Word模闆引擎

繼續閱讀