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>
相关推荐
程序喵大人2 小时前
【C++进阶】STL算法与函数对象 - 02 sort为什么需要随机访问迭代器
开发语言·c++·算法
SomeB1oody2 小时前
【RustyML入门】2.6. 线性判别分析
开发语言·后端·机器学习·rust·教程
程序员雷欧2 小时前
环形缓冲区深度解析:从基础原理到Disruptor源码的全面剖析
java
xiaoqiMikko2 小时前
Dependabot 面板全绿,不代表你的 Tomcat 没洞
java·spring boot
前端开发张小七3 小时前
Java 学习笔记 · 第三课:多线程与并发编程(线程、同步、死锁、Lock、乐观锁与悲观锁)
java·后端·程序员
用户昵称1003 小时前
C/C++编程-工程实践-本地存储log的工程意义
c语言·开发语言
花生了什么事o3 小时前
JVM 垃圾回收:对象如何被判定和回收
java·jvm
evans在进步3 小时前
HashMap 为什么线程不安全?ConcurrentHashMap 如何解决?
java·spring boot·spring
我命由我123454 小时前
匈牙利命名法
java·服务器·后端·学习·java-ee·kotlin·学习方法
闲猫4 小时前
LangChain / Integrations / Integrations by component / Tool
java·数据库·langchain