package com.havenliu.document;
import java.io.*;
import java.util.*;
import freemarker.template.*;
public class DocumentHandler {
private Configuration cfg;
public DocumentHandler() {
cfg = new Configuration(Configuration.VERSION_2_3_31);
cfg.setDefaultEncoding("UTF-8");
}
public void createDoc() {
// 1. 準備資料模型
Map<String, Object> dataMap = new HashMap<>();
prepareData(dataMap);
// 2. 設定模板載入路徑(classpath 下的 template 套件)
cfg.setClassForTemplateLoading(this.getClass(), "/com/havenliu/document/template");
// 3. 取得模板
Template template;
try {
template = cfg.getTemplate("test.ftl");
} catch (IOException e) {
throw new RuntimeException("載入模板失敗", e);
}
// 4. 輸出 Word 檔案
File outFile = new File("D:/temp/outFile.doc");
try (Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"))) {
template.process(dataMap, out);
} catch (IOException | TemplateException e) {
throw new RuntimeException("生成文件失敗", e);
}
}
/** 建構資料模型:Key 必須與模板參數對應 */
private void prepareData(Map<String, Object> dataMap) {
dataMap.put("author", "張三");
dataMap.put("remark", "這是測試備註資訊");
// 表格 1 資料
List<Table1> table1 = new ArrayList<>();
table1.add(new Table1("2010-10-1", "制定 10 月開發計畫內容。"));
table1.add(new Table1("2010-10-2", "開會討論開發計畫"));
dataMap.put("table1", table1);
// 表格 2 資料
List<Table2> table2 = new ArrayList<>();
for (int i = 0; i < 5; i++) {
table2.add(new Table2(
"測試開發計畫" + i,
"張三——" + i,
"2010-10-1",
"2010-10-31",
"備註資訊"
));
}
dataMap.put("table2", table2);
}
// 內部資料類別(Getter/Setter 省略)
static class Table1 { String date, text; Table1(String d, String t) { date=d; text=t; } }
static class Table2 { String detail, person, beginDate, finishDate, remark;
Table2(String d, String p, String b, String f, String r) { detail=d; person=p; beginDate=b; finishDate=f; remark=r; } }
}
setClassForTemplateLoading 從 classpath 載入,適合打包成 JAR 部署;亦支援檔案系統、ServletContext、資料庫等來源。Map/POJO,模板專注排版邏輯,修改版面無需動 Java 代碼。UTF-8,避免中文亂碼。IOException(IO 錯誤)與 TemplateException(模板語法/資料錯誤),便於診斷。${var}、#list、#if)DocumentHandler 載入模板、注入資料、輸出 .doc生成的 Word 完全符合 Office 標準,支援複雜表格、圖片、分頁、樣式,列印不變形,滿足企業級報表需求。