效果如下图:
第一步:编辑模板
第二步:另存为xml格式
第三步:改为ftl模板格式,并编辑ftl模板
郑加威
郑加威
2
0
2014-12-08T12:37:00Z
2014-12-08T12:37:00Z
1
30
177
微软中国
1
1
206
15
<#list tbs as tb>
tc \l 1 "Lists of Objects"
Table ${tb.tb_name}
表名
${tb.tb_name}
注释
${tb.tb_comment}
字段名
注释
数据类型
Pimary Key
Mandatory
Foreign Key
Unique
Check
<#list cols as col>
${col.col_name}
${col.col_comment}
${col.col_datatype}
${col.p_enable}
${col.m_enable}
${col.f_enable}
${col.u_enable}
${col.c_enable}
#list>
索引
<#list idxs as idx>
${idx.contents};
#list>
#list>
编写java代码:
package org.cnzjw.word1;
public class Col {
private String tb_name;
private String col_name;
private String col_comment;
private String col_datatype;
private String p_enable;
private String m_enable;
private String f_enable;
private String u_enable;
private String c_enable;
public String getTb_name() {
return tb_name;
}
public void setTb_name(String tb_name) {
this.tb_name = tb_name;
}
public String getCol_name() {
return col_name;
}
public void setCol_name(String col_name) {
this.col_name = col_name;
}
public String getCol_comment() {
return col_comment;
}
public void setCol_comment(String col_comment) {
this.col_comment = col_comment;
}
public String getCol_datatype() {
return col_datatype;
}
public void setCol_datatype(String col_datatype) {
this.col_datatype = col_datatype;
}
public String getP_enable() {
return p_enable;
}
public void setP_enable(String p_enable) {
this.p_enable = p_enable;
}
public String getM_enable() {
return m_enable;
}
public void setM_enable(String m_enable) {
this.m_enable = m_enable;
}
public String getF_enable() {
return f_enable;
}
public void setF_enable(String f_enable) {
this.f_enable = f_enable;
}
public String getU_enable() {
return u_enable;
}
public void setU_enable(String u_enable) {
this.u_enable = u_enable;
}
public String getC_enable() {
return c_enable;
}
public void setC_enable(String c_enable) {
this.c_enable = c_enable;
}
}
package org.cnzjw.word1;
public class Table {
private String no;
private String tb_name;
private String tb_comment;
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getTb_name() {
return tb_name;
}
public void setTb_name(String tb_name) {
this.tb_name = tb_name;
}
public String getTb_comment() {
return tb_comment;
}
public void setTb_comment(String tb_comment) {
this.tb_comment = tb_comment;
}
}
package org.cnzjw.word1;
public class Idx {
private String contents;
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
}
package org.cnzjw.word1;
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 WordHandler {
private Configuration configuration = null;
public WordHandler() {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}
public void data2word() {
// 要填入模本的数据文件
Map dataMap = new HashMap();
getData(dataMap);
// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以servlet,classpath,数据库装载,
// 这里我们的模板是放在org.cnzjw.template包下面
configuration.setClassForTemplateLoading(this.getClass(),
"/org/cnzjw/template");
Template t = null;
try {
// word.ftl为要装载的模板
t = configuration.getTemplate("word1.ftl");
} catch (IOException e) {
e.printStackTrace();
}
// 输出文档路径及名称
File outFile = new File("D:/outword1.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) {
List tbs = new ArrayList();
for (int j = 0; j < 3; j++) {
Table tb = new Table();
tb.setNo("1." + j);
tb.setTb_name("T_SYS_USER" + j);
tb.setTb_comment("系统用户表" + j);
List cols = new ArrayList();
for (int i = 0; i < 5; i++) {
Col col = new Col();
col.setCol_name("ID" + i);
col.setCol_comment("序号" + i);
col.setCol_datatype("VARCHAR2(20)");
col.setP_enable("Y");
col.setM_enable("Y");
col.setF_enable("N");
col.setU_enable("N");
col.setC_enable("N");
cols.add(col);
}
dataMap.put("cols", cols);
List idxs = new ArrayList();
for (int i = 0; i < 2; i++) {
Idx idx = new Idx();
idx.setContents("CREATE INDEX index_name" + i
+ " on table_name (col_name" + i + ")");
idxs.add(idx);
}
dataMap.put("idxs", idxs);
tbs.add(tb);
dataMap.put("tbs", tbs);
}
}
public static void main(String[] args) {
WordHandler wh = new WordHandler();
wh.data2word();
}
}