poi-tl——Word模板生成器

学习目标

今天,和大家分享一款Word模板引擎,它可以基于Word模板和数据生成新的文档。在工作中我们经常会遇到,将后台的数据填充到特定的word模板中,然后生成渲染模板生成新的word提供下载;例如:学生成绩单,单位合同,报销费用等!如果能够掌控一款合适的java生成word模板的工具,将极大的提高我们的开发效率!


POI-tl介绍

poi-tl(poi template language)是Word模板引擎,基于Word模板和数据生成新的文档。
官方文档:http://deepoove.com/poi-tl

为什么我们选择poi-tl?
在这里插入图片描述
Apache POI不仅封装了易用的文档API(文本、图片、表格、页眉、页脚、图表等),也可以在底层直接操作文档XML结构,poi-tl正是一个基于Apache POI的Word模板引擎,并且拥有着让人喜悦的特性。

快速入门案例

1、添加依赖

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

2、主要代码

private static Logger logger = LoggerFactory.getLogger(FileUtils.class);
    /**
     *  渲染Word 文件
     * @param templatePath   文件模板路径
     * @param fileDir        生成文件的路径
     * @param fileName       生成的文件名
     * @param paramMap       数据
     * @return
     */
    public static String createWord(String templatePath, String fileDir, String fileName, Map<String, Object> paramMap) {
        File dir = new File(fileDir);
        if (!dir.exists()) {
            logger.info("目录不存在,创建文件夹{}!", fileDir);
            dir.mkdirs();
        }
        String filePath = fileDir +"/"+ fileName;
        XWPFTemplate template = XWPFTemplate.compile(templatePath).render(paramMap);
        try {
            template.writeToFile(filePath);
            template.close();
        } catch (Exception e) {
            logger.error("生成word异常{}", e.getMessage());
            e.printStackTrace();
        }
        return filePath;
    }

3、测试

public static void main(String[] args) throws Exception{
        String templatePath = "E:/template/template.docx";    //模板路径
        String fileDir = "E:/template/final/";  //生成文档路径
        String fileName = "output.docx";   //文件名
        Map<String,Object> params = new HashMap<>();
        params. put("title", "Hi, poi-tl Word模板引擎");
        String wordPath = FileUtils.createWord(templatePath, fileDir, fileName, params);
        System.out.println("生成文档的路径wordPath = " + wordPath);
    }

注:

  • 首先,我们把模板放在了E盘的template文件夹下面。模板里面的内容:
    在这里插入图片描述
  • 运行main方法,将会在E:\template\final生成最终的文件。在这里插入图片描述

渲染文本

准备word模板:
在这里插入图片描述

 public static void main(String[] args) throws Exception{
        String templatePath = "E:/template/template.docx";    //模板路径
        String fileDir = "E:/template/final/";  //生成文档路径
        String fileName = "output.docx";   //文件名
        Map<String,Object> params = new HashMap<>();
        params.put("dep","xxx部门");
        params.put("apply_man","张三");
        params.put("project","xxxxx项目费用");
        params.put("money","13000元");
        params.put("count","3");
        params.put("year","2021");
        params.put("month","03");
        params.put("day","23");
        String wordPath = FileUtils.createWord(templatePath, fileDir, fileName, params);
        System.out.println("生成文档的路径wordPath = " + wordPath);
    }

在这里插入图片描述

循环渲染

我们常常遇到,费用报销明细里面可能有多条记录,此时我们需要用到表格循环。

 public static void main(String[] args) throws Exception{
        String templatePath = "E:/template/template.docx";    //模板路径
        String fileDir = "E:/template/final/";  //生成文档路径
        String fileName = "output.docx";   //文件名
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        Money row01 = new Money();
        row01.setApply_man("张三");
        row01.setProject("xxxx项目费用");
        row01.setCount("1");
        row01.setMoney("2300");
        Money row02 = new Money();
        row02.setApply_man("李四");
        row02.setProject("xxxx项目费用");
        row02.setCount("4");
        row02.setMoney("6800");
        List<Money> moneys = new ArrayList<>();
        moneys.add(row01);
        moneys.add(row02);
        Configure config = Configure.builder()
                .bind("moneys", policy)
                .build();
        XWPFTemplate template = XWPFTemplate.compile(templatePath, config).render(
                new HashMap<String, Object>() {{
                    put("moneys", moneys);
                }}
        );
        template.writeToFile(fileDir+fileName);
        template.close();
    }

在这里插入图片描述

渲染图片

图片标签以@开始:{{@var}}

    public static void main(String[] args) throws Exception {
        Map<String, Object> params = new HashMap<>();
        String templatePath = "E:/template/template.docx";    //模板路径
        String imgPath = "E:/template/template-img.png";
        String fileDir = "E:/template/final/";

        params.put("img", Pictures.ofLocal(imgPath).size(567, 120).create());
        XWPFTemplate template = XWPFTemplate.compile(templatePath).render(params);
        template.writeToFile(fileDir + "output.docx");
        template.close();
    }

最终效果:
在这里插入图片描述


本篇文章到这里就基本结束了,如果这篇文章对你有帮助,希望大家能留下你的点赞、 关注、 分享、 留言❤️❤️❤️

在这里插入图片描述


版权声明:本文为weixin_43759352原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。