Freemarker模板工具

FreemarkerUtil

java 复制代码
package com.util;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class FreemarkerUtil {
    private Configuration configuration = new Configuration();

    public void createDoc(Map<String, Object> dataMap, String outFileName, String inFileName) throws IOException {
        configuration.setClassForTemplateLoading(FreemarkerUtil.class, "/com/util/");
        configuration.setDefaultEncoding("UTF-8");
        Template t = null;
        Writer out = null;
        File outFile = new File(outFileName);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(outFile);
            t = configuration.getTemplate(inFileName, "UTF-8");
            OutputStreamWriter oWriter = new OutputStreamWriter(fos, "UTF-8");
            out = new BufferedWriter(oWriter);

            dataMap.put("sqnyr", getdate());
            t.process(dataMap, out);
            out.close();
            fos.close();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (TemplateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static String getdate() {
        Date nowTime = new Date();
        SimpleDateFormat time = new SimpleDateFormat("yyyy年MM月dd日");
        String date = time.format(nowTime);
        return date;
    }

    public static String getdate1() {
        Date nowTime = new Date();
        SimpleDateFormat time = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = time.format(nowTime);
        return date;
    }
}

demo

java 复制代码
package com.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Version: 1.0
 * @Description:
 */
public class FreemarkerUtilTest {

    public static void main(String[] args) throws Exception {
        FreemarkerUtil util = new FreemarkerUtil();
        Map<String, Object> dataMap = new HashMap<>();

        // 1. 第一(动态行)
        List<Map<String, Object>> firstHeirList = new ArrayList<>();
        Map<String, Object> h1 = new HashMap<>();
        h1.put("name", "");
        h1.put("cardType", "身份证");
        h1.put("cardNo", "1101011011234");
        h1.put("relation", "");
        h1.put("remark", "");
        firstHeirList.add(h1);
        firstHeirList.add(h1);
        firstHeirList.add(h1);
        firstHeirList.add(h1);
        // 可继续添加更多行
        dataMap.put("firstHeirList", firstHeirList);

        // 2. (动态行)
        List<Map<String, Object>> deceasedHeirList = new ArrayList<>();
        Map<String, Object> d1 = new HashMap<>();
        d1.put("name", "");
        d1.put("cardType", "身份证");
        d1.put("cardNo", "1101019999");
        d1.put("relation", "");
        d1.put("remark", "");
        deceasedHeirList.add(d1);
        
        dataMap.put("deceasedHeirList", deceasedHeirList);
        dataMap.put("bianhao", "123468908765434");

        // 生成文档
        util.createDoc(dataMap, "D:\\File\\名单.doc", "MD2.ftl");
    }





}

#动态 ftl

html 复制代码
<html xmlns:o="urn:schemas-microsoft-com:office:office"
      xmlns:w="urn:schemas-microsoft-com:office:word"
      xmlns="http://www.w3.org/TR/REC-html40">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>附件一 全体名单</title>
    <style>
        body { font-family: "宋体"; font-size: 12pt; }
		 .main-title { text-align: center; font-size: 18pt; font-weight: bold; margin: 10px 0; }
        
        table { width: 100%; border-collapse: collapse; margin: 0; }
        table, th, td { border: 1px solid black; }
        th, td { padding: 6px; text-align: center; }
		 td,th { padding:6px; text-align:center; word-break:break-all; white-space:normal; }
        .title-row1 { text-align: center; font-weight: bold; font-size: 13pt; }
		.title-row { text-align: left;  font-size: 13pt; }
        .note-cell { text-align: left; vertical-align: top; padding: 8px; line-height: 1.5; }
    </style>
</head>
<body>

 <!-- 一行显示:左 附件一 | 中 标题 | 右 编号 -->
<table style="width:100%; border:none; margin-bottom:1px;">
  <tr style="border:none;">
    <td style="border:none; text-align:left; font-size:10pt;">附件一</td>
    <td style="border:none; text-align:center; font-size:18pt; font-weight:bold;">全体名单</td>
    <td style="border:none; text-align:right; font-size:10pt;">${applyid!''}</td>
  </tr>
</table>
	
    <!-- 第一名单 -->
    <table>
        <tr>
            <td colspan="6" class="title-row1">第一人名单</td>
        </tr>
        <tr>
            <th>序号</th>
            <th>姓名</th>
            <th>证件类型</th>
            <th>证件号码</th>
            <th>与人<br>关系</th>
            <th>备注</th>
        </tr>
        <#-- 动态生成行,数据传list即可 -->
        <#list firstHeirList as heir>
        <tr>
            <td>${heir_index + 1}</td>
            <td>${heir.name!''}</td>
            <td>${heir.cardType!''}</td>
            <td>${heir.cardNo!''}</td>
            <td>${heir.relation!''}</td>
            <td>${heir.remark!''}</td>
        </tr>
        </#list>
    </table>

    <!-- 附记:表格 -->
    <table>
        <tr>
            <td colspan="6" class="title-row">附记:xxxxx填写到以下表格</td>
        </tr>
        <tr>
            <th>序号</th>
            <th>姓名</th>
            <th>证件类型</th>
            <th>证件号码</th>
            <th>与人<br>关系</th>
            <th>备注</th>
        </tr>
        <#list deceasedHeirList as heir>
        <tr>
            <td>${heir_index + 1}</td>
            <td>${heir.name!''}</td>
            <td>${heir.cardType!''}</td>
            <td>${heir.cardNo!''}</td>
            <td>${heir.relation!''}</td>
            <td>${heir.remark!''}</td>
        </tr>
        </#list>
    </table>

     

    <!-- 填写注意事项表格 -->
    <table>
        <tr>
            <td style="width:20%; font-weight:bold; text-align:center;">填写注意事项</td>
            <td class="note-cell">
                1. <br>
                2. <br>
              
            </td>
        </tr>
    </table>

    <!-- 签名区域 -->
    <div style="margin-top:30px;">
        <p>全体签名:</p>
        <br><br>
        <p>管理人签名(签章):_</p>
        <div style="text-align:right; margin-top:20px;">
           ${sqnyr!''}
        </div>
    </div>
</body>
</html>

固定ftl

xml 复制代码
通过网盘分享的文件:demo.ftl
链接: https://pan.baidu.com/s/1crCDazpJmJyyEfC2DfQCLA?pwd=syh7 提取码: syh7
xml 复制代码
 <!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>
相关推荐
学习,学习,在学习40 分钟前
Qt多线程的使用与注意事项
开发语言·数据库·qt
asdfg125896340 分钟前
`(line1, line2) -> line1 + line2` 此Lambda 表达式的理解
java·开发语言
如竟没有火炬43 分钟前
去除重复字母——贪心+单调栈
开发语言·数据结构·python·算法·leetcode·深度优先
AI人工智能+电脑小能手1 小时前
【大白话说Java面试题 第49题】【JVM篇】第9题:什么是双亲委派机制?介绍一下运作过程。?
java·开发语言·jvm
码农-阿杰1 小时前
Java 线程中断机制深度解析:从 API 到底层 C++ 实现
java·开发语言·c++
Brilliantwxx1 小时前
【C++】priority_queue以及 仿函数 的学习
开发语言·c++·笔记·学习·算法
风味蘑菇干1 小时前
斗地主案例
java·数据结构·算法
码农学院1 小时前
itextsharp .net中如何设置两个表格的间距设为0,取网站的域名,协议、端口、当前站点目录的地址
开发语言·c#·.net
宠..1 小时前
VS Code 修改 C++ 标准同时修改错误检测标准
java·linux·开发语言·javascript·c++·python·qt