• 页面内容导出为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("导出失败");
        }
      } 
  • 相关阅读:
    perl 解json数组
    华为云3大体系化防护实践,保障金融业云上数据安全
    弹性文件服务解密 -- 块存储、文件存储、对象存储的区别
    【nodejs原理&源码赏析(6)】深度剖析cluster模块源码与node.js多进程
    云+AI+5G时代,华为云已准备好多元化云服务架构
    高能街访 | 为什么他们都纷纷为深圳打Call?
    Angularjs进阶笔记(2)—自定义指令中的数据绑定
    Angularjs进阶笔记(1)—不同类型的双向数据绑定
    ServiceComb java-chassis和CSE java-chassis的区别
    使用inspector功能查看和管理契约
  • 原文地址:https://www.cnblogs.com/winkey4986/p/2934009.html
Copyright © 2020-2023  润新知