1. FreeMarker模板引擎的使用: 模板 + 數據模型 = 輸出
1) FreeMarker模板:一個普通文本文件,其中使用了一些FreeMarker的特別標記。
2) 數據模型:存放了數據的數據結構,通常是一個Hash存儲結構(如:HashMap)
3) FreeMarker框架負責將一個數據模型中的數據合併到模板中,從而生成輸出。
2.在pom文件中引入依賴:
<code>
<
dependency
><
groupId
>org.freemarkergroupId
><
artifactId
>freemarkerartifactId
><
version
>2.3.23version
>dependency
>/<code>3.創建模板文件:
將word模板另存為xml格式的文檔,然後將xml文檔後綴名修改為ftl;
模板文件內容展示:
<code>
<
html
><
head
><
meta
http-equiv
="Content-Type"
content
="text/html; charset=UTF-8"
><
title
>Insert title heretitle
>head
><
body
>
h1
>${orgName}-----${status}h1
><
#if
'A
' =="${answer1}"
> ${orgName}........#if
><
#if
'A
' =="${answer2}"
> ${orgName}快成年#if
>body
>html
>/<code>4.工具類測試:
<code>
import
java.io.BufferedWriter;import
java.io.File;import
java.io.FileOutputStream;import
java.io.OutputStreamWriter;import
java.io.Writer;import
java.sql.Time;import
java.util.*;import
org.slf4j.Logger;import
org.slf4j.LoggerFactory;import
freemarker.template.Configuration;import
freemarker.template.Template;public
class
WordExportUtil
{private
static
Logger LOGGER = LoggerFactory.getLogger(WordExportUtil.
class
);private
static
WordExportUtil service =null
;private
WordExportUtil
()
{super
(); }public
static
WordExportUtilgetInstance
()
if
(service ==null
) {synchronized
(WordExportUtil.
class
){if
(service ==null
) { service =new
WordExportUtil(); } } }return
service; }public
FilecreateDocFile
(String templateFilePath,Map dataMap, String exportFilePath,
int
loadType)throws
Exception { Template t =null
; Configuration configuration =new
Configuration(Configuration.VERSION_2_3_22); configuration.setDefaultEncoding("UTF-8"
);try
{ templateFilePath = pathReplace(templateFilePath); String ftlPath = templateFilePath.substring(0
, templateFilePath.lastIndexOf("/"
));if
(loadType ==1
) { configuration.setDirectoryForTemplateLoading(new
File(ftlPath)); }else
{ configuration.setClassForTemplateLoading(this
.getClass(), ftlPath); } String ftlFile = templateFilePath.substring(templateFilePath.lastIndexOf("/"
)+1
); t = configuration.getTemplate(ftlFile); File outFile =new
File(exportFilePath); Writer out =null
; out =new
BufferedWriter(new
OutputStreamWriter(new
FileOutputStream(outFile))); t.process(dataMap, out); }catch
(Exception e) { LOGGER.error("導出word文檔出錯"
, e);throw
e; }return
null
; }private
StringpathReplace
(String path)
{while
(path !=null
&& path.contains(""
)) { path = path.replace(""
,"/"
); }return
path; }public
static
void
main
(String[] args)
{ Map dataMap =new
HashMap(); getData(dataMap); String templateFile ="C:\\Users\\admin\\Desktop\\體檢報告.ftl"
; String exportFile ="C:\\Users\\admin\\Desktop\\體檢報告"
+ Calendar.MILLISECOND +".doc"
;try
{ WordExportUtil.getInstance().createDocFile(templateFile, dataMap, exportFile,1
); }catch
(Exception e) { e.printStackTrace(); } }public
static
void
getData
(Map dataMap)
{ dataMap.put("orgName"
,"******"
); dataMap.put("answer1"
,"A"
); dataMap.put("answer2"
,"A"
); dataMap.put("status"
,"健康狀態"
); } } /<code>5.語法 參考 http://freemarker.foofun.cn/
<code>
${...}
: FreeMarker將會輸出真實的值來替換大括號內的表達式,這樣的表達式被稱為 interpolation(插值)。 FTL 標籤(FreeMarker模板的語言標籤)的名字以 註釋: 註釋和HTML的註釋也很相似, 但是它們使用 <code>