大家好,我是知识追寻者,今天给大家介绍一款word模板生成理器; 工作中经常会遇到这种情况,将后台的数据填充到word模板,然后生渲染模板生成新的word提供下载;比如学生成绩单,单位合同,报销费用等!如果能够掌控一款合适的java生成word模板的工具,将极大的提高我们的开发效率!

官方文档:deepoove.com/poi-tl

为什么选择 poi-tl

引擎功能描述
文本将标签渲染为文本
图片将标签渲染为图片
表格将标签渲染为表格
列表将标签渲染为列表
图表条形图(3D条形图)、柱形图(3D柱形图)、面积图(3D面积图)、折线图(3D折线图)、雷达图、饼图(3D饼图)等图表渲染
If Condition判断隐藏或者显示某些文档内容(包括文本、段落、图片、表格、列表、图表等)
Foreach Loop循环循环某些文档内容(包括文本、段落、图片、表格、列表、图表等)
Loop表格行循环渲染表格的某一行
Loop表格列循环渲染表格的某一列
Loop有序列表支持有序列表的循环,同时支持多级列表
图片替换将原有图片替换成另一张图片
书签、锚点、超链接支持设置书签,文档内锚点和超链接功能
强大的表达式完全支持SpringEL表达式,可以扩展更多的表达式:OGNL, MVEL…
标签定制支持自定义标签前后缀
文本框文本框内标签支持
样式模板即样式,同时代码也可以设置样式
模板嵌套模板包含子模板,子模板再包含子模板
合并Word合并Merge,也可以在指定位置进行合并
用户自定义函数(插件)在文档任何位置执行函数

核心代码,将数据填充入模板生成新的word;

 private static Logger logger = LoggerFactory.getLogger(TemplateController.class);
    * @author lsc
    * @param templatePath word模板文件路径
    * @param fileDir      生成的文件存放地址
    * @param fileName     生成的文件名
    * @param paramMap     参数集合
    * @return 返回word生成的路径
    public static String createWord(String templatePath, String fileDir, String fileName, Map<String, Object> paramMap) {
        Assert.notNull(templatePath, "word模板文件路径不能为空");
        Assert.notNull(fileDir, "生成的文件存放地址不能为空");
        Assert.notNull(fileName, "生成的文件名不能为空");
        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;

渲染字符串

模板 {{value}}

public static void main(String[] args) {
        Map<String, Object> params = new HashMap<>();
        // 渲染文本
        params.put("dep","知识研发中心");
        params.put("apply_man","知识追寻者");
        params.put("project","搭建个人网站 https://zszxz.com/index 费用");
        params.put("money","19998");
        params.put("count","8");
        params.put("year","2020");
        params.put("month","02");
        params.put("day","8");
    	// 模板路径
        String templatePath = "C:/mydata/generator/demo/template.docx";
        // 生成word的路径
        String fileDir = "C:/mydata/generator/demo";
        // 生成word的文件
        String fileName = "zszxz.docx";
        String wordPath = createWord(templatePath, fileDir, fileName, params);
        System.out.println("生成文档路径:" + wordPath);

表格模板 {{#table0}}

public static void main(String[] args) {
        Map<String, Object> params = new HashMap<>();
        params.pables.of(new String[][] {
                        new String[] { "19998", "8" ,"apply_man","搭建个人网站 https://zszxz.com/index 费用"},
                        new String[] { "19998", "8" ,"apply_man","搭建个人网站 https://zszxz.com/index 费用"},
                }).border(TableStyle.BorderStyle.DEFAULT).create
        String templatePath = "C:/mydata/generator/demo/template.docx";
        // 生成word的路径
        String fileDir = "C:/mydata/generator/demo";
        // 生成word的文件
        String fileName = "zszxz.docx";
        String wordPath = createWord(templatePath, fileDir, fileName, params);
        System.out.println("生成文档路径:" + wordPath);

此类渲染的表格存在一些问题不能根据我们定义的模板循环渲染,比如费用报销明细里面多条记录,此时就需要使用到表格循环;

* @author lsc * <p> </p> public class Money { private String apply_man; private String project; private String money; private String count; // 省略set get
public static void main(String[] args) throws IOException {
        HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
        Money money1 = new Money();
        money1.setApply_man("知识追寻者");
        money1.setProject("搭建个人网站 https://zszxz.com/index 费用");
        money1.setCount("8");
        money1.setMoney("19988");
        Money money2 = new Money();
        money2.setApply_man("知识追寻者");
        money2.setProject("参加晚会费用");
        money2.setCount("8");
        money2.setMoney("98000");
        List<Money> moneys = new ArrayList<>();
        moneys.add(money1);
        moneys.add(money2);
        // 插件绑定
        Configure config = Configure.builder()
                .bind("moneys", policy).build();
        String templatePath = "C:/mydata/generator/demo/template.docx";
        XWPFTemplate template = XWPFTemplate.compile(templatePath, config).render(
                new HashMap<String, Object>() {{
                    put("moneys", moneys);
        String filePath = "C:/mydata/generator/demo/zszxz.docx";
        template.writeToFile(filePath);
        template.close();

模板 {{*list}}

public static void main(String[] args) {
        Map<String, Object> params = new HashMap<>();
        params.put("list", Numberings.create("Plug-in grammar",
                "Supports word text, pictures, table...",
                "Not just templates"));
        String templatePath = "C:/mydata/generator/demo/template.docx";
        // 生成word的路径
        String fileDir = "C:/mydata/generator/demo";
        // 生成word的文件
        String fileName = "zszxz.docx";
        String wordPath = createWord(templatePath, fileDir, fileName, params);
        System.out.println("生成文档路径:" + wordPath);

需要 本套教程 word模板 关注公众号:知识追寻者 回复 template 获取调试

分类:
后端
标签: