poi-tl 導入word


poi-tl(poi template language)是Word模板引擎,基於Microsoft Word模板和數據生成新的文檔。
1.添加依賴

    <dependency>
        <groupId>com.deepoove</groupId>
        <artifactId>poi-tl</artifactId>
        <version>1.7.3</version>
    </dependency>

2.定義模板

3.代碼

public static void main(String[] args) throws IOException {
        Map<String, Object> data = new HashMap<>();
        data.put("name", "王小二");
        data.put("id", "45224522155222445555");
        data.put("addr", "廣州天河區。。。");
        data.put("h", "sdfd3dfg");
        data.put("j", "hsdh哈哈哈");
        data.put("k", "sdkhfj好好好");
        data.put("l", "廣州天河區。。。");
        data.put("h", "sdfd3dfg");
        data.put("start_time", "2022-03-17");
        XWPFTemplate template = XWPFTemplate.compile("E:/1.docx").render(data);
        FileOutputStream out;
        out = new FileOutputStream("E:/22.docx");
        template.write(out);
        out.flush();
        out.close();
        template.close();
    }

效果

4.數據也可以是對象

import com.deepoove.poi.XWPFTemplate;

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

public class Test {

    public static void main(String[] args) throws IOException {
        Map<String, Object> data = new HashMap<>();
        Sth sth = new Sth("司天宏", "武漢");
        data.put("sth",sth);
        XWPFTemplate template = XWPFTemplate.compile("F:/template.docx")
                .render(data);
        FileOutputStream out;
        out = new FileOutputStream("F:/template2.docx");
        template.write(out);
        out.flush();
        out.close();
        template.close();

    }

}

class Sth {
    private String name;
        private String addr;

    public Sth(String name, String addr) {
        this.name = name;
        this.addr = addr;
    }
}

template中這么取值

二.標簽
poi-tl是一種 “logic-less” 模板引擎,沒有復雜的控制結構和變量賦值,只有標簽。標簽由前后兩個大括號組成, {undefined{title}} 是標簽, {undefined{?title}} 也是標簽, title 是這個標簽的名稱, ? 標識了標簽類型,接下來我們來看看有哪些默認標簽類型(用戶可以創建新的標簽類型,這屬於更高級的話題)。
2.1.文本
模板中這樣寫


數據模型:
String :文本
TextRenderData :有樣式的文本
HyperLinkTextRenderData :超鏈接文本
Object :調用 toString() 方法轉化為文本

代碼

Map<String, Object> data = new HashMap<>();
        data.put("author", new TextRenderData("000000", "Sayi"));
// 超鏈接
        data.put("link",
                new HyperLinkTextRenderData("website", "http://deepoove.com"));
// 錨點
        data.put("anchor",
                new HyperLinkTextRenderData("anchortxt", "anchor:appendix1"));
        XWPFTemplate template = XWPFTemplate.compile("F:/template.docx")
                .render(data);
        FileOutputStream out;
        out = new FileOutputStream("F:/template2.docx");
        template.write(out);
        out.flush();
        out.close();
        template.close();

效果

圖片標簽以@開始:{undefined{@var}}
模板示例 {undefined{@local}}

代碼

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.HyperLinkTextRenderData;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.util.BytePictureUtils;

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

public class Test {

    public static void main(String[] args) throws IOException {
        Map<String, Object> data = new HashMap<>();
        // 本地圖片
        data.put("local", new PictureRenderData(80, 100, "F:/圖片.jpg"));

// 圖片流
       // data.put("localbyte", new PictureRenderData(80, 100, ".png", new FileInputStream("./logo.png")));

// 網絡圖片(注意網絡耗時對系統可能的性能影響)
       // data.put("urlpicture", new PictureRenderData(50, 50, ".png", BytePictureUtils.getUrlBufferedImage("http://deepoove.com/images/icecream.png")));

// java 圖片
        //data.put("bufferimage", new PictureRenderData(80, 100, ".png", bufferImage)));
        XWPFTemplate template = XWPFTemplate.compile("F:/template.docx")
                .render(data);
        FileOutputStream out;
        out = new FileOutputStream("F:/template2.docx");
        template.write(out);
        out.flush();
        out.close();
        template.close();

    }

}

效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM