java freemarker(xml模板) 导出word

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

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

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

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

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

 

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

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

 

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

<!--StartFragment -->
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<Dljtsgjbxxb> {
 /**
  *
  */
 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";
  }
  
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值