页面内容导出为word(多浏览器)

1.所用jar包:freemarker-2.3.1.jar
 
2.创建模版template.ftl。如:${content}
 
后台ToWordUtil.java
 
public class ToWordUtil {
 private static Configuration configuration = null;
 
 public ToWordUtil() {
 
 }
 
 /*
  * public static void main(String[] args) { ToWordUtil to = new
  * ToWordUtil(); to.createDoc(); }
  */
 
 public static void createDoc(String tmpPath, ServletContext path,
   Map dataMap, String year) {
  configuration = new Configuration();
 
  configuration.setDefaultEncoding("gbk");
  // 要填入模本的数据文件
 
  // 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,
 
  // 这里我们的模板是放在com.supcon.szwh.util.template包下面
  // configuration.setClassForTemplateLoading(this.getClass(),
  // "../template");
  configuration.setServletContextForTemplateLoading(path, "\\template");
  Template t = null;
 
  try {
 
   // test.ftl为要装载的模板
 
   t = configuration.getTemplate("template.ftl");
 
   t.setEncoding("gbk");
 
  } catch (IOException e) {
 
   e.printStackTrace();
 
  }
 
  // 输出文档路径及名称
  File outFile = new File(tmpPath + "苏州古典园林" + year + "年年报.doc");
 
  Writer out = null;
 
  try {
 
   out = new BufferedWriter(new OutputStreamWriter(
 
   new FileOutputStream(outFile), "gbk"));
 
  } catch (Exception e1) {
 
   e1.printStackTrace();
 
  }
 
  try {
 
   t.process(dataMap, out);
 
   out.close();
 
  } catch (TemplateException e) {
 
   e.printStackTrace();
 
  } catch (IOException e) {
 
   e.printStackTrace();
 
  }
 
 }
 
}
 
调用方法:
 
Report report = reportService.getById(id);
Map dataMap = new HashMap();
dataMap.put("content", report.getContent());
ToWordUtil.createDoc(tmpPath, request.getSession().getServletContext(), dataMap, report.getYear());
 
页面调用:
 
var options = { 
         success:   showResponse, // post-submit callback 
         url:       "${rc.contextPath}/report/resent?id="+$!{report.id},        // 创建临时的word文件
         type:      "post" ,     // 'get' or 'post', override for form's 'method' attribute 
         dataType:  "json"      // 'xml', 'script', or 'json' (expected server response type) 
      };
  var options2 = { 
         url:       "${rc.contextPath}/report/removeFile?year="+$!{report.year},        // 删除临时存储的word文件 
         type:      "post" ,     // 'get' or 'post', override for form's 'method' attribute 
         dataType:  "json"      // 'xml', 'script', or 'json' (expected server response type) 
      };
     
     //注册导出按钮事件
     $("#resentButton").click(function(){
       $("#myform").ajaxStart(function(){
        }).ajaxSubmit(options);
     });
  
  //ajax回调函数
     function showResponse(responseText, statusText, xhr, $form)  { 
   if(responseText=="resent_success"){
   window.document.location.href="/doc/苏州古典园林$!{report.year}年年报.doc";//下载word文件
   //window.document.forms['myform'].action="${rc.contextPath}/report/removeFile?year="+$!{report.year};
         //window.document.forms['myform'].submit();
   $("#myform").ajaxStart(function(){
        }).ajaxSubmit(options2);
  }else{
   alert("导出失败");
    }
  } 
原文地址:https://www.cnblogs.com/winkey4986/p/2934009.html