大概思路以及步骤,本人在网上看了别人的例子,自己也照着修改了下 用在了实际的应用开发过程中,效果真的很不错
先用office2003或者2007编辑好word的样式,然后另存为xml,将xml翻译为FreeMarker模板,最后用java来解析FreeMarker模板并输出Doc。经测试这样方式生成的word文档完全符合office标准,样式、内容控制非常便利,打印也不会变形,生成的文档和office中编辑文档完全一样
将编辑好的文档另存为XML
再用Firstobjectfree XML editor将xml中我们需要填数据的地方打上FreeMarker标记
最后生成的文档样式
package com.havenliu.document;
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.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 DocumentHandler {
private Configuration configuration = null;
public DocumentHandler() {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
}
public void createDoc() {
//要填入模本的数据文件
Map dataMap=new HashMap();
getData(dataMap);
//设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
//这里我们的模板是放在com.havenliu.document.template包下面
configuration.setClassForTemplateLoading(this.getClass(), "/com/havenliu/document/template");
Template t=null;
try {
//test.ftl为要装载的模板
t = configuration.getTemplate("test.ftl");
} catch (IOException e) {
e.printStackTrace();
}
//输出文档路径及名称
File outFile = new File("D:/temp/outFile.doc");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
} catch (FileNotFoundException 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("author", "张三");
dataMap.put("remark", "这是测试备注信息");
List
_table1=new ArrayList();
Table1 t1=new Table1();
t1.setDate("2010-10-1");
t1.setText("制定10月开发计划内容。");
_table1.add(t1);
Table1 t2=new Table1();
t2.setDate("2010-10-2");
t2.setText("开会讨论开发计划");
_table1.add(t2);
dataMap.put("table1", _table1);
List
_table2=new ArrayList();
for(int i=0;i<5;i++)
{
Table2 _t2=new Table2();
_t2.setDetail("测试开发计划"+i);
_t2.setPerson("张三——"+i);
_t2.setBegindate("2010-10-1");
_t2.setFinishdate("2010-10-31");
_t2.setRemark("备注信息");
_table2.add(_t2);
}
dataMap.put("table2", _table2);
}
}