- 导入freemarker.jar包
- 把word文档另存为xml格式,2007以上版本支持。
- 编写代码,把路径更改为xml所在路径。
- 把需要更改的地方写成${}形式。
package Document.src.com.havenliu.document;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import sun.misc.BASE64Encoder;
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<String, Object> dataMap = new HashMap<String, Object>();
getData(dataMap);
// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
// 这里我们的模板是放在com.havenliu.document.template包下面
configuration.setClassForTemplateLoading(this.getClass(), "template");
Template t = null;
try {
// test.ftl为要装载的模板
t = configuration.getTemplate("test.ftl");
} catch (IOException e) {
e.printStackTrace();
}
// 输出文档路径及名称
File outFile = new File("D:/temp/动态页眉.doc");
Writer out = null;
try {
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile), "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} 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<String, Object> dataMap) {
String a="<w:sectPr wsp:rsidR='' wsp:rsidRPr='' wsp:rsidSect=''>"
+"<w:hdr w:type='odd'>"
+"<wx:pBdrGroup>"
+"<wx:borders>"
+"<wx:bottom wx:val='solid' wx:bdrwidth='15' wx:space='1' wx:color='auto'/>"
+"</wx:borders>"
+"<w:p wsp:rsidR='1' wsp:rsidRDefault='008B1696'>"
+"<w:pPr>"
+"<w:pStyle w:val='a4'/>"
+"</w:pPr>"
+"<w:r>"
+"<w:t>Q</w:t>"
+"</w:r>"
+"<w:r>"
+"<w:rPr>"
+"<w:rFonts w:hint='fareast'/>"
+"</w:rPr>"
+"<w:t>weqwe</w:t>"
+"</w:r>"
+"</w:p>"
+"</wx:pBdrGroup>"
+"</w:hdr>"
+"<w:pgSz w:w='11906' w:h='16838'/>"
+"<w:pgMar w:top='623' w:right='1800' w:bottom='1440' w:left='1800' w:header='851' w:footer='992' w:gutter='0'/>"
+"<w:cols w:space='720'/>"
+"<w:docGrid w:type='lines' w:line-pitch='312'/>"
+"</w:sectPr>";
// 页码
// dataMap.put("page123", "<w:fldSimple w:instr=' PAGE
// \\*MERGEFORMAT'><w:rw:rsidR='009D2538'w:rsidRPr='009D2538'><w:rPr><w:noProof/><w:langw:val='zh-CN'/></w:rPr><w:t>2</w:t></w:r></w:fldSimple>");
dataMap.put("asd","");
dataMap.put("page", "首页");
dataMap.put("author", "这是测试备注信息");
dataMap.put("remark", "这是测试备注信息");
List<Table1> _table1 = new ArrayList<Table1>();
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> _table2 = new ArrayList<Table2>();
for (int i = 0; i < 6; 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);
}
// 动态图片
// public String getImageStr() {
// String imgFile = "d:/temp/test.jpg";
// InputStream in = null;
// byte[] data = null;
// try {
// in = new FileInputStream(imgFile);
// data = new byte[in.available()];
// in.read(data);
// in.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// BASE64Encoder encoder = new BASE64Encoder();
// return encoder.encode(data);
//
// }
}
测试类:
package Document.src.com.havenliu.document;
import java.util.ArrayList;
import java.util.List;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
DocumentHandler dh=new DocumentHandler();
dh.createDoc();
// System.out.println(dh.getImageStr());
List list = new ArrayList();
}
}