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>
相关推荐
狼爷8 小时前
吃透 Java Function 接口,搞定 99% 的 Stream 场景
java·函数式编程
祎雪双十Gy12 小时前
从 DataX 的配置加载说起:我用 FastJson2 做了一个轻量级动态配置管理库
java·后端
小锋java123412 小时前
分享一套锋哥原创的SpringBoot4+Vue3宠物领养网站系统
java
考虑考虑16 小时前
Java实现hmacsha1加密算法
java·后端·java ee
掉鱼的猫16 小时前
Spring Boot → Solon 注解迁移实战指南:一张对照表说清楚
java·spring boot
plainGeekDev16 小时前
广播接收器 → Flow + Lifecycle
android·java·kotlin
plainGeekDev16 小时前
EventBus → SharedFlow
android·java·kotlin
带刺的坐椅17 小时前
Spring Boot → Solon 注解迁移实战指南:一张对照表说清楚
java·springboot·web·solon
用户37215742613517 小时前
Java 将一个 PPT 文档拆分为多个文件
java
人活一口气1 天前
Spring Boot与AIGC的完美结合:从零搭建智能内容生成平台
java·spring boot·aigc