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的注释也很相似, 但是它们使用
分享到:
關鍵字: FreeMarker 数据模型 使用