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>
相关推荐
啃瓜子的松鼠12 分钟前
泛微OAe9-后端二开异步调用
java
我是唐青枫20 分钟前
Kotlin Delegation 委托详解
开发语言·kotlin
qq_4069810325 分钟前
共享变量一 关于extern与const
开发语言·c++
FRing031 小时前
JavaScript入门到精通三十二(深拷贝、浅拷贝、递归函数)
开发语言·前端·javascript
秋天的怀念_David1 小时前
第二十一夜 · 数字黑洞与西西弗斯的惩罚
java·程序人生·算法
外收内放1 小时前
Python基础语法练习题(73)
java·前端·python
Wang's Blog1 小时前
Java 项目实战: 交友项目 - 环信后台发送消息测试
java·交友
用户3721574261352 小时前
Java 操作 Word 脚注和尾注:添加、读取与删除
java
小嘉丶学长2 小时前
Day14 · 字典与集合
java·开发语言·网络·python·学习·1024程序员节
字节探索2 小时前
Spring Boot 接口幂等性设计:从唯一索引到 Redis+Lua 的工程实践
java·spring boot