FreeMarker的简单使用
2015-09-02 14:54
267 查看
1、模板文件采用xml形式,比如word可以另存为xml格式文件,目前已知模板文件可以是xml、html、jsp等,生成模板要注意,尽量用低版本的软件来生成xml模板。
java示例代码,读取D:/workspace/testWebService/WebRoot/resource/template目录的模板,动态生成word,动态设置word内容中的姓名、性别、爱好
package com.fxf.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreeMarkerUtil {
//模板存放目录
private final static String templateDir = "D:/workspace/testWebService/WebRoot/resource/template";
//生成文件目录
private final static String outFile = "D:/workspace/testWebService/WebRoot/resource/tmp/";
/**
* 从D:/workspace/testWebService/WebRoot/resource/template路径获取指定模板
* @param templateName
* @return Template
*/
public static Template getFreeMarkerTemplate(String templateName) {
Template template = null;
try {
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
configuration.setDirectoryForTemplateLoading(new File(templateDir));
//根据名称获取模板
template = configuration.getTemplate(templateName);
} catch (IOException e) {
e.printStackTrace();
}
return template;
}
/**
* @param args
*/
public static void main(String[] args) {
//根据模板名称获取模板
Template template = getFreeMarkerTemplate("模板标题.xml");
//设置数据,map的key与模板中的key一致
Map map = new HashMap();
map.put("name", "王五");
map.put("sex", "女");
map.put("like", "篮球");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile + System.currentTimeMillis() + ".doc"), "UTF-8"));
//生成文件,并将数据填充到文件中
template.process(map, out);
out.flush();
out.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例代码
java示例代码,读取D:/workspace/testWebService/WebRoot/resource/template目录的模板,动态生成word,动态设置word内容中的姓名、性别、爱好
package com.fxf.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class FreeMarkerUtil {
//模板存放目录
private final static String templateDir = "D:/workspace/testWebService/WebRoot/resource/template";
//生成文件目录
private final static String outFile = "D:/workspace/testWebService/WebRoot/resource/tmp/";
/**
* 从D:/workspace/testWebService/WebRoot/resource/template路径获取指定模板
* @param templateName
* @return Template
*/
public static Template getFreeMarkerTemplate(String templateName) {
Template template = null;
try {
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
configuration.setDirectoryForTemplateLoading(new File(templateDir));
//根据名称获取模板
template = configuration.getTemplate(templateName);
} catch (IOException e) {
e.printStackTrace();
}
return template;
}
/**
* @param args
*/
public static void main(String[] args) {
//根据模板名称获取模板
Template template = getFreeMarkerTemplate("模板标题.xml");
//设置数据,map的key与模板中的key一致
Map map = new HashMap();
map.put("name", "王五");
map.put("sex", "女");
map.put("like", "篮球");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile + System.currentTimeMillis() + ".doc"), "UTF-8"));
//生成文件,并将数据填充到文件中
template.process(map, out);
out.flush();
out.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例代码
相关文章推荐
- NodeJS介绍
- ODI调用WebService---->OdiInvokeWebService
- Ubuntu Linux系统下的SVN客户端工具PySVN
- linux 下的cat 命令
- C++模板的申明和定义
- 关于位域的理解
- poj 3084 Panic Room 【最小割】
- Xcode 根据要显示的内容和内容字体,自适应调整UILabel的大小
- cocostudio的BitmapLabel字体替换
- JGibbLDA:java版本的LDA(Latent Dirichlet Allocation)实现、修改及使用
- Delphi2010下的FillChar
- ubuntu下如何安装及使用 pysvn-workbench
- 使用npm国内镜像
- maven+cxf+spring
- 精简指令集与复杂指令集
- 替换手机号码中间X位转成*号
- 循环26~28
- 学习总结---ADO编程中_ConnectionPtr的open函数连接字符串
- pinyin4j工具类
- 从Hadoop到Spark的架构实践