---
title: "java freemarker（xml模板） 导出word "
author: "xustar"
date: "2011-11-29T16:10:52Z"
tags: []
categories: ["Web前端"]
slug: "java-freemarker-xml-word"
draft: false
lastmod: "2011-11-29T16:20:37Z"
id: 1284104
url: "https://xustar.iteye.com/blog/1284104"
---

最近，由于项目的需要要导出规范样式的word文档，且要求较高。

之前已经尝试过两种方式（

1.使用POI在后台写出WORD，该方法和基础，写简单格式很ok，打样式复杂的时候，就纠结了，反正我是没调整好，放弃。

2.将内容传到页面，在用特殊的格式后，将页面另存为DOC格式的文档。这种方法很简单，几乎都都在客户端页面解决，但是另存后，其中html标签转在word中显示主要的问题是很多框线显示出来了，相当的不好看。

）

以上两种方法确定可以实现word导出，但是word样式没有达到客户要求。

 

因此，在网上查找得到可以使用freemarker的模板来搞定，极度兴奋。

总结一下关键步骤，供大家参考。

 

1.将处理好的的word模板，另存为xml格式的文档。并在其中添加参数：  ${export}

&lt;!--StartFragment --&gt;

2.将xml文档修改完成后，保存并修改后缀名为“\*\*\*\*.ftl”;

 

3.编写java类，主要实现对模板的赋值以及生成流，用于导出。

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.UnsupportedEncodingException;  
import java.io.Writer;   
import java.text.DateFormat;  
import java.text.SimpleDateFormat;  
import java.util.Date;  
import java.util.HashMap;  
import java.util.Map; 

import com.cist.bayonet.mainstat.model.Dljtsgjbxxb;  
import freemarker.template.Configuration;  
import freemarker.template.Template;  
import freemarker.template.TemplateException;  
/\**  
 * @fun 重大事故word导出类  
 * @author xzl  
 *  
 \*/  
public class export\_word {    
   
 private Configuration configuration = null;  
 public export\_word() {       
  configuration = new Configuration();     
  configuration.setDefaultEncoding("utf-8");  
  //System.out.print("========");  
  }    
 public File createDoc(Dljtsgjbxxb dlb,String spth) throws UnsupportedEncodingException {        //要填入模本的数据文件   
  Map dataMap=new HashMap();      
  getData(dataMap,dlb);        //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet，classpath，数据库装载，  
  //这里我们的模板是放在com.cist.bayonet.mainstat.service.impl包下面    
  configuration.setClassForTemplateLoading(this.getClass(), "/com/cist/bayonet/mainstat/service/impl"); 

  Template t=null;  
  try {           //sgxxb\_info.ftl为要装载的模板       
   t = configuration.getTemplate("sgxxb\_info.ftl");    
   }  
  catch (IOException e) {      
   e.printStackTrace();       
   }        //输出文档路径及名称     
  File outFile = new File(spth+"/.doc");  
  Writer out = null;     
  try {      
   FileOutputStream fos = new FileOutputStream(outFile);  
   OutputStreamWriter oWriter = new OutputStreamWriter(fos,"UTF-8");//这个地方对流的编码不可或缺，

使用main（）单独调用时，应该可以，但是如果是web请求导出时导出后word文档就会打不开，并且包XML文件错误。主要是编码格式不正确，无法解析。  
   out = new BufferedWriter(oWriter);    
   } catch (FileNotFoundException e1) {  
    e1.printStackTrace();       
    }       
   try {           
    t.process(dataMap, out);  
    } catch (TemplateException e) {    
     e.printStackTrace();      
     } catch (IOException e) {     
      e.printStackTrace();      
      }  
             return outFile;   
     }

 /\**     * 注意dataMap里存放的数据Key值要与模板中的参数相对应     * @param dataMap     \*/    
 @SuppressWarnings("unchecked")  
 private void getData(Map dataMap,Dljtsgjbxxb db)     {  //这里就是给模板的的Map赋值，通过接受的实体对象来获取所需的值。测试时建议直接用常量，如dataMap.put("jbdw1", "绵阳市二大队");

  dataMap.put("jbdw1", db.getJbdw1());  
  dataMap.put("jbdw2", db.getJbdw2());  
  dataMap.put("jbdw3", db.getJbdw3());  
  dataMap.put("sjfs1", db.getJbsjfs1().toString().substring(0, 19)+db.getJbsjfs1f());  
  dataMap.put("sjfs2", db.getJbsjfs2().toString().substring(0, 19)+db.getJbsjfs2f());  
  dataMap.put("sjfs3", db.getJbsjfs3().toString().substring(0, 19)+db.getJbsjfs3f());  
  dataMap.put("jbyxm1", db.getJbr1());  
  dataMap.put("jbyxm2", db.getJbr2());  
  dataMap.put("jbyxm3", db.getJbr3());  
  dataMap.put("sbpzr1", db.getSbpzr1());  
  dataMap.put("sbpzr2", db.getSbpzr2());  
  dataMap.put("sbpzr3", db.getSbpzr3());  
  dataMap.put("xxsbr1", db.getSbr1());  
  dataMap.put("xxsbr2", db.getSbr2());  
  dataMap.put("xxsbr3", db.getSbr3());  
  dataMap.put("sbsjfs1", db.getSbsjfs1().toString().substring(0, 19)+db.getSbsjfs1f());  
  dataMap.put("sbsjfs2", db.getSbsjfs2().toString().substring(0, 19)+db.getSbsjfs2f());  
  dataMap.put("sbsjfs3", db.getSbsjfs3().toString().substring(0, 19)+db.getSbsjfs3f());  
  dataMap.put("dcsw", db.getDcsw());  
  dataMap.put("szrs", db.getXlbm());  
  dataMap.put("qjwx", db.getQjwxsw());  
  dataMap.put("zsrs", db.getSs());  
  dataMap.put("jyqk", db.getJbqk());  
  dataMap.put("jbsj", db.getJbsj().toString().substring(0, 19));  
  dataMap.put("zby", db.getZby());  
  dataMap.put("sghsqk", db.getSgxxhsqk());  
  DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:MM:ss");  
  String string = dFormat.format(new Date());  
  dataMap.put("export", string);  
  }   
//      public static void main(String args\[]){  
//       export\_word word = new export\_word();  
//       word.createDoc();  
//           }  
 }

4.由于我使用的是SSH框架，所以struts处理Action部分如下：

（一下类由于内容较多只贴了有用的部分，个人觉得重点的部分的result注解中的设定）

@ParentPackage("json-default")  
@Namespace("/struts/mainstat")  
@Results({  
 @Result(name="word",type="stream",params={"contentDisposition","attachment;filename=${filename}","bufferSize","4096","contentType","application/octet-stream;charset=utf-8","inputName","inputStream"}),  
 @Result(name="msg",location="/bayonet\_jsp/result.jsp"),  
 @Result(name="alltoll",type="json"),  
 @Result(name="repeat",type="json",params={"includeProperties","m\_StringResult","noCache","true","ignoreHierarchy","false"}),  
})  
public class MainstartAction extends BasePageAction&lt;Dljtsgjbxxb&gt; {

 /\**  
  *  
  \*/  
 private static final long serialVersionUID = 1L;  
 private static final Logger lr = LoggerFactory.getLogger(MainstartAction.class);  
 @Resource(name = "MainstartService")  
 private IMainstartService m\_css;  
 private int cont\_a;  
 private String cityString;  
 private Dljtsgjbxxb dlb;   
 private FileInputStream inputStream = null;  
 private String filename = "SGXXBG" + java.util.Calendar.getInstance().get(java.util.Calendar.MILLISECOND) +".doc";

 

。。。。。。。。。。。。。。。。。。。。。。。。

/\**  
  * WORD导出  
  * @return  
  \*/  
 @Action("word\_accident")  
 public String exportWord() {  
  try{  
   ServletContext sc = ServletActionContext.getServletContext();  
   String path = sc.getRealPath("/bayonet\_file/word/");//获取服务器端，暂时存储路径  
   dlb = m\_css.getEntity(dlb);  
         export\_word word = new export\_word();  
         File file = word.createDoc(dlb, path);//返回的是，已经使用模板生成的word文档的路径  
   if (file.exists()) {  
    this.inputStream = new FileInputStream(file);//Action需要返回的流  
   } else {  
    m\_StringResult = Messages.MSG\_FAIL;

  return "msg";  
   }  
   return "word";//通过上面对应的注解返回stream；客户端可以在线打开或者下载。   
  }catch(Exception ex){  
   m\_StringResult = Messages.MSG\_SEARCH\_FAILTURE;  
   lr.error(ex.getMessage());  
   return "msg";  
  }  
    
 }