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();

    }

}

效果

posted @ 2022-03-17 15:47  LemonSquash  阅读(864)  评论(0编辑  收藏  举报