首先向客户获得一份平时需要的word模板,将需要用程序填写的数据使用能够区分的字母,如XXXXX。
第二步就是把word模板(必须采用word2003制作模板),转化为xml文件(word可以另存为xml的),用EditorPlus等工具打开,注意字符编码,word的字符编码为utf-8,所以打开是最好是先不要改动,先看编码是否正确(不正确的改下字符编码),然后格式化一下xml文件,格式化后方便阅读(格式化,网上有很多在线格式化工具),然后寻找你刚才在word中留下的标记,把他们换成freemarker标记,其语法跟struts语法基本一致。
比如:
1、向freemarker传递一个对象,提取一个对象的属性值可以写成${report.planName}。
2、向freemarker传递一个集合,遍历一个集合可以:
<#listtable1_tr as table1_td> //遍历开始
${table1_td} //输出值
</#list> //遍历结束
3、如果遍历一个二维的集合
4、定义一个变量<#assignx=0 />
5、判断对象是否为空
<#ifQA.question??>
${QA.question}
</#if>
第三步输出word,如:
示例
Word模板是这样的
XMl是这样的:
接下来我们使用freemarker标记性语言对动态变化的数据进行处理,由简到难,我们先替换列表数据之外的数据, 每个标记不相同就行。如:
接下来我们替换列表数据,使用<#list>标签进行替换
在列表数据开头写<#list>
结尾写</#list>
替换中间部分
然后保存:
我的xml的保存文件名叫test2.xml.等下会用到这个
接下来开始写程序:
首先你要有这些;就是freemarker的开源包,百度一个,导入就好,
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
TheFreemarker类:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class TheFreemarker {
private Configuration configuration = null;
public TheFreemarker() {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}
public void createDoc() {
// 要填入模本的数据文件
Map dataMap = new HashMap();
getData(dataMap);
// 设置模本装置方法和路径
// 这里我们的模板是放在src.model包下面
configuration.setClassForTemplateLoading(this.getClass(),
"model");
Template t = null;
try {
t = configuration.getTemplate("test2.xml"); // 装载test2.xml模板
} catch (IOException e) {
e.printStackTrace();
}
// 输出文档路径及名称
File outFile = new File("D:/outFileDoc.doc");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile),"utf-8"));
} catch (Exception e1) {
e1.printStackTrace();
}
try {
t.process(dataMap, out);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 注意dataMap里存放的数据Key值要与模板中的参数相对应
* @param dataMap
*/
private void getData(Map dataMap) {
dataMap.put("name", "小新与小白");//姓名 xml里的标记为${name}
dataMap.put("Tdate", "2011-12-02");//时间 xml里的标记为${Tdate}
dataMap.put("address", "北京海淀区");//时间 xml里的标记为${address}
List table2 = new ArrayList();
for (int i = 0; i < 5; i++) {
Table2 t = new Table2();
t.setApplyno("BBBBBBBB-BB");
t.setCustname("小新");
t.setLoandate("2012-12-12");
t.setRegion("999-999");
table2.add(t);
}
dataMap.put("table2", table2);
}
Table2 为:
public class Table2 {
private String applyno;
private String custname;
private String loandate;
private String region;
public String getApplyno() {
return applyno;
}
public void setApplyno(String applyno) {
this.applyno = applyno;
}
public String getCustname() {
return custname;
}
public void setCustname(String custname) {
this.custname = custname;
}
public String getLoandate() {
return loandate;
}
public void setLoandate(String loandate) {
this.loandate = loandate;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
测试类:
public class Pptest {
public static void main(String[] args) {
TheFreemarker tf=new TheFreemarker();
tf.createDoc();
}
}
执行结果:
说明
公用事业呼叫中心的质检简报模板制作,需要保证每个数据的名字不变,如果想改变需要自行修改后台代码及相应的实体类
实体类:com.xxxx.vo.entity.cc.repo.QCBriefReport
com.xxxx.vo.entity.cc.repo.QCBriefReportQATable
相关文章
freemarker导出word——让表格数据行数 列数自动变化
freemarker导出word——跟fusioncharts结合生成图、文word