`
jinvasshole
  • 浏览: 769336 次
文章分类
社区版块
存档分类
最新评论

freemarkerExportWord.java freemarker+xml导出Word文档

 
阅读更多

package cn.tempus.gongdan.utils;
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.HashMap;
import java.util.Map;
import org.apache.axis.utils.StringUtils;
import cn.tempus.gongdan.domain.User;
import freemarker.template.Configuration;
import freemarker.template.Template;

/**
* @ClassName: ExportWord
* @Description: 导出Word文档
* @author cheng.wang
* date 2012-2-7
*/
public class ExportWord {
private Configuration configuration = null;
public ExportWord(){
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}

public File createDoc(Integer id,User user,String path){
// 要填入模本的数据文件
Map<String, Object> dataMap = new HashMap<String, Object>();
getData(dataMap,id,user);
// 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
// 这里我们的模板是放在cn.tempus.gongdan.template包下面
configuration.setClassForTemplateLoading(this.getClass(),"/cn/tempus/gongdan/template");
Template t = null;
try {
//chupiaotongzhi.ftl为要装载的模板
t = configuration.getTemplate("chupiaotongzhi.ftl");
} catch (IOException e) {
e.printStackTrace();
}
// 输出文档路径及名称
File outFile = new File(path);
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8"));
t.process(dataMap, out);
} catch (Exception e) {
e.printStackTrace();
}
return outFile;
}
/**
* 注意dataMap里存放的数据Key值要与模板中的参数相对应
*
* @param dataMap
*/
private void getData(Map<String, Object> dataMap,Integer id,User user) {
dataMap.put("orderid", "0"+id);
dataMap.put("truename", user.getTrueName());
dataMap.put("year",String.valueOf(CalendarUtil.getYear()));
dataMap.put("month", CalendarUtil.getMonth());
dataMap.put("day", CalendarUtil.getDayOfMonth());
dataMap.put("phone", StringUtils.isEmpty( user.getSeatNumber())?"未知":user.getSeatNumber());
dataMap.put("department", user.getDepartment().getName());
}
public static void main(String[] args) {
long start=System.currentTimeMillis();
ExportWord dh=new ExportWord();
//dh.createDoc();
long end=System.currentTimeMillis();
System.out.println(end-start);
}
}


/**
* @Description: 导出出票通知word文档
* @param @param id
* @param @param response
* @return void
*/
@RequestMapping(value = "/chupiaotongzhi/{id}")
public void exportWord(@PathVariable("id") Integer id,HttpServletResponse response){
byte b[] = new byte[1024]; // the file to download.
User user = (User) getSession().getAttribute("currentUser");
ExportWord word = new ExportWord();
File outFile = word.createDoc(id,user,GlobalStatic.uploadFilePath3+"chupiaotongzhi.doc");
FileInputStream in = null;
OutputStream o = null;
try {
in = new FileInputStream(outFile);
o = response.getOutputStream();
response.setContentType("application/x-tar");
response.setHeader("Content-disposition", "attachment; filename="
+ URLEncoder.encode("出票通知书0"+id+".doc", "UTF-8"));// 指定下载的文件名
response.setHeader("Content_Length",String.valueOf( outFile.length())); // download the file.
int n = 0;
while ((n = in.read (b))!= -1)
{
o.write(b, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
in.close();
o.flush();
o.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?mso-application progid="Word.Document"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml"
xmlns:v="urn:schemas-microsoft-com:vml" xmlns:w10="urn:schemas-microsoft-com:office:word"
xmlns:sl="http://schemas.microsoft.com/schemaLibrary/2003/core"
xmlns:aml="http://schemas.microsoft.com/aml/2001/core" xmlns:wx="http://schemas.microsoft.com/office/word/2003/auxHint"
xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xmlns:wsp="http://schemas.microsoft.com/office/word/2003/wordml/sp2"
w:macrosPresent="no" w:embeddedObjPresent="no" w:ocxPresent="no"
xml:space="preserve"><w:ignoreElements
w:val="http://schemas.microsoft.com/office/word/2003/wordml/sp2" /><o:DocumentProperties><o:Author>deshid</o:Author><o:LastAuthor>deshid</o:LastAuthor><o:Revision>2</o:Revision><o:TotalTime>0</o:TotalTime><o:Created>2012-02-07T05:45:00Z</o:Created><o:LastSaved>2012-02-07T05:45:00Z</o:LastSaved><o:Pages>1</o:Pages><o:Words>3</o:Words><o:Characters>20</o:Characters><o:Lines>1</o:Lines><o:Paragraphs>1</o:Paragraphs><o:CharactersWithSpaces>22</o:CharactersWithSpaces><o:Version>11.0000</o:Version></o:DocumentProperties><w:fonts><w:defaultFonts
w:ascii="Times New Roman" w:fareast="宋体" w:h-ansi="Times New Roman"
w:cs="Times New Roman" /><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="楷体_GB2312"><w:panose-1
w:val="02010609030101010101" /><w:charset w:val="86" /><w:family
w:val="Modern" /><w:pitch w:val="fixed" /><w:sig w:usb-0="00000001"
w:usb-1="080E0000" w:usb-2="00000010" w:usb-3="00000000" w:csb-0="00040000"
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:font w:name="@楷体_GB2312"><w:panose-1
w:val="02010609030101010101" /><w:charset w:val="86" /><w:family
w:val="Modern" /><w:pitch w:val="fixed" /><w:sig w:usb-0="00000001"
w:usb-1="080E0000" w:usb-2="00000010" w:usb-3="00000000" w:csb-0="00040000"
w:csb-1="00000000" /></w:font></w:fonts><w:styles><w:versionOfBuiltInStylenames
w:val="4" /><w:latentStyles w:defLockedState="off"
w:latentStyleCount="156" /><w:style w:type="paragraph"
w:default="on" w:styleId="a"><w:name w:val="Normal" /><wx:uiName
wx:val="正文" /><w:pPr><w:widowControl w:val="off" /><w:jc
w:val="both" /></w:pPr><w:rPr><wx:font wx:val="Times New Roman" /><w:kern
w:val="2" /><w:sz w:val="21" /><w:sz-cs w:val="24" /><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:semiHidden /></w:style><w:style
w:type="table" w:default="on" w:styleId="a1"><w:name w:val="Normal Table" /><wx:uiName
wx:val="普通表格" /><w:semiHidden /><w:rPr><w:rFonts w:fareast="Times New Roman" /><wx:font
wx:val="Times New Roman" /></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:semiHidden /></w:style><w:style w:type="paragraph"
w:styleId="a3"><w:name w:val="header" /><wx:uiName wx:val="页眉" /><w:basedOn
w:val="a" /><w:pPr><w:pStyle w:val="a3" /><w:pBdr><w:bottom
w:val="single" w:sz="6" wx:bdrwidth="15" w:space="1" w:color="auto" /></w:pBdr><w:tabs><w:tab
w:val="center" w:pos="4153" /><w:tab w:val="right" w:pos="8306" /></w:tabs><w:snapToGrid
w:val="off" /><w:jc w:val="center" /></w:pPr><w:rPr><wx:font
wx:val="Times New Roman" /><w:sz w:val="18" /><w:sz-cs
w:val="18" /></w:rPr></w:style><w:style w:type="paragraph"
w:styleId="a4"><w:name w:val="footer" /><wx:uiName wx:val="页脚" /><w:basedOn
w:val="a" /><w:pPr><w:pStyle w:val="a4" /><w:tabs><w:tab
w:val="center" w:pos="4153" /><w:tab w:val="right" w:pos="8306" /></w:tabs><w:snapToGrid
w:val="off" /><w:jc w:val="left" /></w:pPr><w:rPr><wx:font
wx:val="Times New Roman" /><w:sz w:val="18" /><w:sz-cs
w:val="18" /></w:rPr></w:style></w:styles><w:shapeDefaults><o:shapedefaults
v:ext="edit" spidmax="2050" /><o:shapelayout v:ext="edit"><o:idmap
v:ext="edit" data="1" /></o:shapelayout></w:shapeDefaults><w:docPr><w:view
w:val="print" /><w:zoom w:percent="75" /><w:bordersDontSurroundHeader /><w:bordersDontSurroundFooter /><w:attachedTemplate
w:val="" /><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:useFELayout /></w:compat><wsp:rsids><wsp:rsidRoot
wsp:val="008D7E9A" /><wsp:rsid wsp:val="008D7E9A" /><wsp:rsid
wsp:val="00E21298" /></wsp:rsids></w:docPr><w:body><wx:sect><w:p
wsp:rsidR="00000000" wsp:rsidRDefault="00E21298"><w:r><w:pict><v:shapetype
id="_x0000_t202" coordsize="21600,21600" o:spt="202"
path="m,l,21600r21600,l21600,xe"><v:stroke joinstyle="miter" /><v:path
gradientshapeok="t" o:connecttype="rect" /></v:shapetype><v:shape
id="_x0000_s1026" type="#_x0000_t202"
style="position:absolute;left:0;text-align:left;margin-left:126pt;margin-top:69.15pt;width:225pt;height:46.8pt;z-index:1"
stroked="f"><v:textbox><w:txbxContent><w:p wsp:rsidR="00000000"
wsp:rsidRDefault="00E21298"><w:pPr><w:jc w:val="distribute" /><w:rPr><w:sz
w:val="72" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="fareast" /><wx:font
wx:val="宋体" /><w:sz w:val="72" /></w:rPr><w:t>出票通知书</w:t></w:r></w:p></w:txbxContent></v:textbox></v:shape></w:pict></w:r><w:r><w:pict><v:shape
id="_x0000_s1029" type="#_x0000_t202"
style="position:absolute;left:0;text-align:left;margin-left:9pt;margin-top:-15.6pt;width:459pt;height:78pt;z-index:3"
stroked="f"><v:textbox><w:txbxContent><wx:pBdrGroup><wx:borders><wx:bottom
wx:val="solid" wx:bdrwidth="15" wx:space="1" wx:color="auto" /></wx:borders><w:p
wsp:rsidR="00000000" wsp:rsidRDefault="00E21298"><w:pPr><w:pStyle w:val="a3" /><w:jc
w:val="distribute" /><w:rPr><w:rFonts w:fareast="楷体_GB2312" /><w:sz
w:val="48" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:fareast="楷体_GB2312"
w:hint="fareast" /><wx:font wx:val="楷体_GB2312" /><w:sz
w:val="48" /></w:rPr><w:t>深圳市昼夜通实业发展有限公司</w:t></w:r></w:p><w:p
wsp:rsidR="00000000" wsp:rsidRDefault="00E21298"><w:pPr><w:pStyle w:val="a3" /><w:jc
w:val="distribute" /><w:rPr><w:sz w:val="28" /></w:rPr></w:pPr><w:r><w:rPr><w:sz
w:val="28" /></w:rPr><w:t>SHENZHEN DAYNITOM INDUSTRY DEVELOPMENT CO., LTD</w:t></w:r></w:p><w:p
wsp:rsidR="00000000" wsp:rsidRDefault="00E21298"><w:pPr><w:pStyle w:val="a3" /></w:pPr></w:p></wx:pBdrGroup><w:p
wsp:rsidR="00000000" wsp:rsidRDefault="00E21298" /></w:txbxContent></v:textbox></v:shape></w:pict></w:r></w:p><w:p
wsp:rsidR="00000000" wsp:rsidRDefault="00E21298" /><w:p wsp:rsidR="00000000"
wsp:rsidRDefault="00E21298" /><w:p wsp:rsidR="00000000"
wsp:rsidRDefault="00E21298" /><w:p wsp:rsidR="00000000"
wsp:rsidRDefault="00E21298" /><w:p wsp:rsidR="00000000"
wsp:rsidRDefault="00E21298" /><w:p wsp:rsidR="00000000"
wsp:rsidRDefault="00E21298"><w:pPr><w:wordWrap w:val="off" /><w:jc
w:val="right" /><w:rPr><w:rFonts w:ascii="Arial" w:h-ansi="Arial"
w:cs="Arial" /><wx:font wx:val="Arial" /><w:sz w:val="28" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts
w:ascii="Arial" w:h-ansi="Arial" w:cs="Arial" w:hint="fareast" /><wx:font
wx:val="宋体" /><w:sz w:val="28" /></w:rPr><w:t>№</w:t></w:r><w:r><w:rPr><w:rFonts
w:ascii="Arial" w:h-ansi="Arial" w:cs="Arial" /><wx:font
wx:val="Arial" /><w:sz w:val="28" /></w:rPr><w:t> ${orderid}</w:t></w:r></w:p><w:p
wsp:rsidR="00E21298" wsp:rsidRDefault="00E21298"><w:r><w:pict><v:shape
id="_x0000_s1028" type="#_x0000_t202"
style="position:absolute;left:0;text-align:left;margin-left:-9pt;margin-top:444.6pt;width:477pt;height:163.8pt;z-index:2"
stroked="f"><v:textbox><w:txbxContent><w:p wsp:rsidR="00000000"
wsp:rsidRDefault="00E21298"><w:pPr><w:ind w:first-line-chars="2000" /><w:rPr><w:sz
w:val="28" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts w:hint="fareast" /><wx:font
wx:val="宋体" /><w:sz w:val="28" /></w:rPr><w:t>署名:${truename}</w:t></w:r></w:p><w:p
wsp:rsidR="00000000" wsp:rsidRDefault="00E21298"><w:pPr><w:ind
w:first-line-chars="2000" /><w:rPr><w:sz w:val="28" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts
w:hint="fareast" /><wx:font wx:val="宋体" /><w:sz w:val="28" /></w:rPr><w:t>日期:</w:t></w:r><w:r><w:rPr><w:rFonts
w:hint="fareast" /><w:sz w:val="28" /></w:rPr><w:t>${year}</w:t></w:r><w:r><w:rPr><w:rFonts
w:hint="fareast" /><wx:font wx:val="宋体" /><w:sz w:val="28" /></w:rPr><w:t>年</w:t></w:r><w:r><w:rPr><w:rFonts
w:hint="fareast" /><w:sz w:val="28" /></w:rPr><w:t>${month}</w:t></w:r><w:r><w:rPr><w:rFonts
w:hint="fareast" /><wx:font wx:val="宋体" /><w:sz w:val="28" /></w:rPr><w:t>月</w:t></w:r><w:r><w:rPr><w:rFonts
w:hint="fareast" /><w:sz w:val="28" /></w:rPr><w:t>${day}</w:t></w:r><w:r><w:rPr><w:rFonts
w:hint="fareast" /><wx:font wx:val="宋体" /><w:sz w:val="28" /></w:rPr><w:t>日</w:t></w:r></w:p><w:p
wsp:rsidR="00000000" wsp:rsidRDefault="00E21298"><w:pPr><w:ind
w:first-line-chars="2000" /><w:rPr><w:sz w:val="28" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts
w:hint="fareast" /><wx:font wx:val="宋体" /><w:sz w:val="28" /></w:rPr><w:t>电话:</w:t></w:r><w:r><w:rPr><w:rFonts
w:hint="fareast" /><w:sz w:val="28" /></w:rPr><w:t>(0755)</w:t></w:r><w:r><w:rPr><w:rFonts
w:hint="fareast" /><wx:font wx:val="宋体" /><w:sz w:val="28" /></w:rPr><w:t>${phone}</w:t></w:r></w:p><w:p
wsp:rsidR="00000000" wsp:rsidRDefault="00E21298"><w:pPr><w:ind
w:first-line-chars="2000" /><w:rPr><w:sz w:val="28" /></w:rPr></w:pPr></w:p><w:p
wsp:rsidR="00000000" wsp:rsidRDefault="00E21298"><w:pPr><w:pStyle w:val="a4" /><w:jc
w:val="center" /><w:rPr><w:sz w:val="36" /></w:rPr></w:pPr><w:r><w:rPr><w:rFonts
w:hint="fareast" /><wx:font wx:val="宋体" /><w:color
w:val="999999" /><w:sz w:val="36" /></w:rPr><w:t>${department}:深圳市福田保税区桃花路</w:t></w:r><w:r><w:rPr><w:rFonts
w:hint="fareast" /><w:color w:val="999999" /><w:sz w:val="36" /></w:rPr><w:t>9</w:t></w:r><w:r><w:rPr><w:rFonts
w:hint="fareast" /><wx:font wx:val="宋体" /><w:color
w:val="999999" /><w:sz w:val="36" /></w:rPr><w:t>号腾邦国际物流大厦</w:t></w:r><w:r><w:rPr><w:sz
w:val="36" /></w:rPr><w:t> </w:t></w:r></w:p><w:p wsp:rsidR="00000000"
wsp:rsidRDefault="00E21298" /></w:txbxContent></v:textbox></v:shape></w:pict></w:r><w:r><w:rPr><w:rFonts
w:ascii="Arial" w:h-ansi="Arial" w:cs="Arial" /><wx:font
wx:val="Arial" /><w:sz w:val="28" /></w:rPr><w:t>TO:</w:t></w:r></w:p><w:sectPr
wsp:rsidR="00E21298"><w:pgSz w:w="11906" w:h="16838" /><w:pgMar
w:top="1440" w:right="1106" w:bottom="1440" w:left="1260" w:header="851"
w:footer="992" w:gutter="0" /><w:cols w:space="425" /><w:docGrid
w:type="lines" w:line-pitch="312" /></w:sectPr></wx:sect></w:body></w:wordDocument>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics