package test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
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 com.thoughtworks.xstream.core.util.Base64Encoder;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class Exportdoc {
//配置
private Configuration configuration = null;
public Exportdoc() {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}
/*
* modelName:模版名称
* exportFilePath:文件路径
*
*/
public void createdoc(String modelName, String exportFilePath){
Template template = null;
//导入模版
configuration.setClassForTemplateLoading(this.getClass(), "");
try {
template = configuration.getTemplate(modelName);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Map<String, Object> dataMap = new HashMap<String, Object>();
getData(dataMap);
//导出文件
//输出流
File outfile = new File(exportFilePath);
Writer writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outfile),"utf-8"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
template.process(dataMap, writer);
} catch (TemplateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//把数据打包成map;dataMap里存放的数据Key值要与模板中的参数相对应
private void getData(Map<String, Object> dataMap) {
dataMap.put("author", "shan");
dataMap.put("remark", "这是测试备注信息");
dataMap.put("image", getImageStr());
List<table> table1 = new ArrayList<table>();
table t1 = new table();
t1.setDate("2015-11-16");
t1.setText("测试信息1");
table1.add(t1);
table t2 = new table();
t2.setDate("2015-11-16");
t2.setText("测试信息2");
table1.add(t2);
//放入map集合
dataMap.put("table", table1);
//创建list集合
List<table1> table = new ArrayList<table1>();
//循环存储数据
for (int i = 0; i < 10; i++) {
table1 t = new table1();
t.setSequence(i + 1 + "");
t.setDetail("测试计划" + i);
t.setPerson("shan"+ i);
t.setBegindate("2015-11-1");
t.setFinishdate("2015-11-31");
t.setDescription("备注信息"+i);
table.add(t);
}
//添加至集合
dataMap.put("table1", table);
}
//添加图片
private String getImageStr() {
String imgFile = "E:/Workspaces/ExportDoc/bin/test/test1.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);
}
//测试
public static void main(String[] args) {
Exportdoc ex = new Exportdoc();
//模版位置
String modelName = "masterplate.xml";
//导出位置
String filepath = "D:/demo.doc";
ex.createdoc(modelName, filepath);
System.out.println("导出成功"+filepath);
}
}
package test;
public class table {
private String date;
private String text;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
package test;
public class table1 {
private String sequence;
private String detail;
private String person;
private String description;
private String begindate;
private String finishdate;
public String getSequence() {
return sequence;
}
public void setSequence(String sequence) {
this.sequence = sequence;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
public String getPerson() {
return person;
}
public void setPerson(String person) {
this.person = person;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getBegindate() {
return begindate;
}
public void setBegindate(String begindate) {
this.begindate = begindate;
}
public String getFinishdate() {
return finishdate;
}
public void setFinishdate(String finishdate) {
this.finishdate = finishdate;
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:aml="http://schemas.microsoft.com/aml/2001/core"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xmlns:ve="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2"
xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core"
w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no"
xml:space="preserve"><w:ignoreSubtree
w:val="http://schemas.microsoft.com/office/word/2003/wordml/sp2" /><o:DocumentProperties><o:Author>yq</o:Author><o:LastAuthor>yq</o:LastAuthor><o:Revision>2</o:Revision><o:TotalTime>4</o:TotalTime><o:Created>2015-11-16T08:09:00Z</o:Created><o:LastSaved>2015-11-16T08:09:00Z</o:LastSaved><o:Pages>1</o:Pages><o:Words>31</o:Words><o:Characters>179</o:Characters><o:Lines>1</o:Lines><o:Paragraphs>1</o:Paragraphs><o:CharactersWithSpaces>209</o:CharactersWithSpaces><o:Version>12</o:Version>
</o:DocumentProperties><w:fonts><w:defaultFonts w:ascii="Calibri"
w:fareast="宋体" w:h-ansi="Calibri" w:cs="Times New Roman" /><w:font
w:name="Times New Roman"><w:panose-1
w:val="02020603050405020304" /><w:charset w:val="00" /><w:family
w:val="Roman" /><w:pitch w:val="variable" /><w:sig
w:usb-0="20002A87" w:usb-1="80000000" w:usb-2="00000008"
w:usb-3="00000000" w:csb-0="000001FF" w:csb-1="00000000" />
</w:font><w:font w:name="宋体"><w:altName w:val="SimSun" /><w:panose-1
w:val="02010600030101010101" /><w:charset w:val="86" /><w:family
w:val="auto" /><w:pitch w:val="variable" /><w:sig
w:usb-0="00000003" w:usb-1="080E0000" w:usb-2="00000010"
w:usb-3="00000000" w:csb-0="00040001" w:csb-1="00000000" />
</w:font><w:font w:name="Cambria Math"><w:panose-1
w:val="02040503050406030204" /><w:charset w:val="00" /><w:family
w:val="Roman" /><w:pitch w:val="variable" /><w:sig
w:usb-0="A00002EF" w:usb-1="420020EB" w:usb-2="00000000"
w:usb-3="00000000" w:csb-0="0000009F" w:csb-1="00000000" />
</w:font><w:font w:name="Calibri"><w:panose-1
w:val="020F0502020204030204" /><w:charset w:val="00" /><w:family
w:val="Swiss" /><w:pitch w:val="variable" /><w:sig
w:usb-0="A00002EF" w:usb-1="4000207B" w:usb-2="00000000"
w:usb-3="00000000" w:csb-0="0000009F" w:csb-1="00000000" />
</w:font><w:font w:name="@宋体"><w:panose-1 w:val="02010600030101010101" /><w:charset
w:val="86" /><w:family w:val="auto" /><w:pitch
w:val="variable" /><w:sig w:usb-0="00000003"
w:usb-1="080E0000" w:usb-2="00000010" w:usb-3="00000000"
w:csb-0="00040001" w:csb-1="00000000" />
</w:font>
</w:fonts><w:styles><w:versionOfBuiltInStylenames w:val="7" /><w:latentStyles
w:defLockedState="off" w:latentStyleCount="267"><w:lsdException
w:name="Normal" /><w:lsdException w:name="heading 1" /><w:lsdException
w:name="heading 2" /><w:lsdException w:name="heading 3" /><w:lsdException
w:name="heading 4" /><w:lsdException w:name="heading 5" /><w:lsdException
w:name="heading 6" /><w:lsdException w:name="heading 7" /><w:lsdException
w:name="heading 8" /><w:lsdException w:name="heading 9" /><w:lsdException
w:name="caption" /><w:lsdException w:name="Title" /><w:lsdException
w:name="Subtitle" /><w:lsdException w:name="Strong" /><w:lsdException
w:name="Emphasis" /><w:lsdException w:name="No Spacing" /><w:lsdException
w:name="List Paragraph" /><w:lsdException
w:name="Quote" /><w:lsdException w:name="Intense Quote" /><w:lsdException
w:name="Subtle Emphasis" /><w:lsdException
w:name="Intense Emphasis" /><w:lsdException
w:name="Subtle Reference" /><w:lsdException
w:name="Intense Reference" /><w:lsdException
w:name="Book Title" /><w:lsdException
w:name="TOC Heading" />
</w:latentStyles><w:style w:type="paragraph" w:default="on"
w:styleId="a"><w:name w:val="Normal" /><wx:uiName
wx:val="正文" /><w:rsid w:val="00607186" /><w:pPr><w:widowControl
w:val="off" /><w:jc w:val="both" />
</w:pPr><w:rPr><wx:font wx:val="Calibri" /><w:kern w:val="2" /><w:sz
w:val="21" /><w:sz-cs w:val="22" /><w:lang
w:val="EN-US" w:fareast="ZH-CN" w:bidi="AR-SA" />
</w:rPr>
</w:style><w:style w:type="character" w:default="on" w:styleId="a0"><w:name
w:val="Default Paragraph Font" /><wx:uiName
wx:val="默认段落字体" />
</w:style><w:style w:type="table" w:default="on" w:styleId="a1"><w:name
w:val="Normal Table" /><wx:uiName wx:val="普通表格" /><w:rPr><wx:font
wx:val="Calibri" /><w:lang w:val="EN-US"
w:fareast="ZH-CN" w:bidi="AR-SA" />
</w:rPr><w:tblPr><w:tblInd w:w="0" w:type="dxa" /><w:tblCellMar><w:top
w:w="0" w:type="dxa" /><w:left w:w="108"
w:type="dxa" /><w:bottom w:w="0" w:type="dxa" /><w:right
w:w="108" w:type="dxa" />
</w:tblCellMar>
</w:tblPr>
</w:style><w:style w:type="list" w:default="on" w:styleId="a2"><w:name
w:val="No List" /><wx:uiName wx:val="无列表" />
</w:style><w:style w:type="table" w:styleId="a3"><w:name
w:val="Table Grid" /><wx:uiName wx:val="网格型" /><w:basedOn
w:val="a1" /><w:rsid w:val="00515999" /><w:rPr><wx:font
wx:val="Calibri" />
</w:rPr><w:tblPr><w:tblInd w:w="0" w:type="dxa" /><w:tblBorders><w:top
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0"
w:color="000000" /><w:left w:val="single"
w:sz="4" wx:bdrwidth="10" w:space="0" w:color="000000" /><w:bottom
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0"
w:color="000000" /><w:right w:val="single"
w:sz="4" wx:bdrwidth="10" w:space="0" w:color="000000" /><w:insideH
w:val="single" w:sz="4" wx:bdrwidth="10" w:space="0"
w:color="000000" /><w:insideV w:val="single"
w:sz="4" wx:bdrwidth="10" w:space="0" w:color="000000" />
</w:tblBorders><w:tblCellMar><w:top w:w="0" w:type="dxa" /><w:left
w:w="108" w:type="dxa" /><w:bottom w:w="0"
w:type="dxa" /><w:right w:w="108" w:type="dxa" />
</w:tblCellMar>
</w:tblPr>
</w:style>
</w:styles><w:shapeDefaults><o:shapedefaults
spidmax="2050" /><o:shapelayout ><o:idmap
data="1" />
</o:shapelayout>
</w:shapeDefaults><w:docPr><w:view w:val="print" /><w:zoom
w:percent="100" /><w:doNotEmbedSystemFonts /><w:bordersDontSurroundHeader /><w:bordersDontSurroundFooter /><w:proofState
w:spelling="clean" w:grammar="clean" /><w:defaultTabStop
w:val="420" /><w:drawingGridVerticalSpacing w:val="156" /><w:displayHorizontalDrawingGridEvery
w:val="0" /><w:displayVerticalDrawingGridEvery w:val="2" /><w:punctuationKerning /><w:characterSpacingControl
w:val="CompressPunctuation" /><w:optimizeForBrowser /><w:validateAgainstSchema /><w:saveInvalidXML
w:val="off" /><w:ignoreMixedContent w:val="off" /><w:alwaysShowPlaceholderText
w:val="off" /><w:compat><w:spaceForUL /><w:balanceSingleByteDoubleByteWidth /><w:doNotLeaveBackslashAlone /><w:ulTrailSpace /><w:doNotExpandShiftReturn /><w:adjustLineHeightInTable /><w:breakWrappedTables /><w:snapToGridInCell /><w:wrapTextWithPunct /><w:useAsianBreakRules /><w:dontGrowAutofit /><w:useFELayout />
</w:compat><wsp:rsids><wsp:rsidRoot wsp:val="00515999" /><wsp:rsid
wsp:val="00385AD6" /><wsp:rsid wsp:val="004361F0" /><wsp:rsid
wsp:val="00515999" /><wsp:rsid wsp:val="00607186" /><wsp:rsid
wsp:val="00816293" /><wsp:rsid wsp:val="00872D9D" /><wsp:rsid
wsp:val="009C3897" /><wsp:rsid wsp:val="00AF1C3B" /><wsp:rsid
wsp:val="00D764A5" /><wsp:rsid wsp:val="00FB62F1" />
</wsp:rsids>
</w:docPr><w:body><w:p wsp:rsidR="00BC65CD" wsp:rsidRDefault="00515999"
wsp:rsidP="00385AD6"><w:pPr><w:jc w:val="center" /><w:rPr><w:b /><w:sz
w:val="28" /><w:sz-cs w:val="28" />
</w:rPr>
</w:pPr><w:r wsp:rsidRPr="00385AD6"><w:rPr><w:rFonts w:hint="fareast" /><w:b /><w:sz
w:val="28" /><w:sz-cs w:val="28" />
</w:rPr><w:t>2015</w:t>
</w:r><w:r wsp:rsidRPr="00385AD6