JAVA將ftl模板轉word文檔超詳細步驟

因爲需求要生成複雜的word,所以用模板實現下載

一:首先要創建一個word文檔模板,將文檔另存爲xml格式保存,再把xml格式文件修改後綴爲ftl文件。

word文檔模板:
在這裏插入圖片描述在這裏插入圖片描述
轉ftl文件:直接將xml文件重命名
在這裏插入圖片描述

二:java後臺主要代碼

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

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

/**
 * Created by Newbie on 2018/12/28.
 */
public class WordUtils {

  public static File createWord(Map<String, Object> dataMap,String templateName,String filePath) {
    //創建文件
    String name = (int) (Math.random() * 100000) + ".doc";
    File file = new File(filePath + templateName + name);
    try {
      // 創建配置實例
      Configuration configuration = new Configuration();

      // 設置編碼
      configuration.setDefaultEncoding("UTF-8");

      // 設置處理空值
      configuration.setClassicCompatible(true);

      // 設置ftl模板文件加載方式(我是將ftl模板文件放在項目中/resources/template包下的)
      configuration.setClassForTemplateLoading(WordUtils.class, "/template");

      // 將模板和數據模型合併生成文件
      Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
      // 獲取模板
      Template template = configuration.getTemplate(templateName+".ftl");
      // 生成文件
      template.process(dataMap, out);

      // 清空緩存
      out.flush();
      // 關閉流
      out.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return file;
  }


  public static void main(String[] args) {
    Map map = new HashMap();
    map.put("name","孫儷");
    map.put("updateTime","2020-02-27");
    map.put("education","博士");
    map.put("job","人事經理");
    map.put("yearsOfWorking",5);
    map.put("age",35);
    map.put("address","上海");
    map.put("mobile","17625456852");
    map.put("email","[email protected]");

    List<Map> list = new ArrayList<>();
    Map map1 = new HashMap();
    map1.put("startDate", "2010-05-31");
    map1.put("endDate", "2014-03-02");
    map1.put("schoolName", "清華大學");
    map1.put("major", "管理");
    map1.put("educationName", "博士");
    list.add(map1);
    Map map2 = new HashMap();
    map2.put("startDate", "2006-05-31");
    map2.put("endDate", "2010-03-02");
    map2.put("schoolName", "清華大學");
    map2.put("major", "管理");
    map2.put("educationName", "本科");
    list.add(map2);
    map.put("educationList",list);

    createWord(map, "resume", "D:\\template\\");
  }
}


三:編輯ftl文件,獲取動態值。

在這裏插入圖片描述
打開文件時無法下手修改,我是用的idea快捷鍵Ctrl + Alt + L(不區分大小寫)將代碼格式化,如果無效應該是快捷鍵衝突了,可以看看是不是網易音樂或者QQ等等其它軟件快捷鍵。格式化之後就很好修改了,支持FreeMarker基本標籤。

在這裏插入圖片描述

FTL常用標籤及語法:

  1. 判斷對象是否存在 。如: obj可以是任何類型的對象,像集合,實體類,屬性等等
    <#if obj??>…</#if>
    <#if obj??> …<#else>…</#if>
    或者
    <#if obj?exists>…</#if>
    <#ifobj?exists> …<#else>…</#if>
  2. 判斷是否和某一個值相等
    <#if obj?exists && obj.id==1>…</#if>
    <#if obj?exists && obj.id == 1>…<#else>…</#if>
    注: 必須先判斷是否存在,纔可比較相等,如果該指定的參數不存在還比較相等的話就回出錯;
  3. 獲取對象值
    獲取普通屬性值:${(obj.屬性名稱)!} 或者 ${屬性名稱}
    獲取日期類值:${obj.屬性名稱?string(“yyyy-MM-dd HH:mm:ss”)}
    獲取金額類值(以數字20爲例):
    <#setting number_format=“percent”/> // 設置數字默認輸出方式(‘percent’,百分比)
    <#assign answer=20/>        // 聲明變量 answer 20
    ${answer?string}          // 轉換字符串輸出 4,200%
    ${answer?string.number}     // 轉換數字輸出 42
    ${answer?string.currency}      // 轉換貨幣輸出 ¥42.00
    ${answer?string.percent}     // 轉換百分比輸出 4,200%
  4. 集合
    遍歷集合:
    <#list empList! as emp>
    ${emp.name!}
    </#list>
    .
    可以這樣遍歷集合:
    <#list 0…(empList!?size-1) as i>
    ${empList[i].name!}
    </#list>
    .
    與jstl循環類似,也可以訪問循環的狀態。
    empList?size    // 取集合的長度
    emp_index:     // int類型,當前對象的索引值
    emp_has_next:    // boolean類型,是否存在下一個對象
    .
    使用<#break>跳出循環
    <#if emp_index = 0><#break></#if>
    .
    集合長度判斷
    <#if empList?size != 0></#if> // 判斷=的時候,注意只要一個=符號,而不是==

四:最後運行看效果

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章