zoukankan      html  css  js  c++  java
  • webservse导出excel和word

             //导出EXCEL
            function game_Excel() {
                var platname =encodeURI( $("#slectplat").val());//加密(是为了防止有中文)
                $("#ExcelForm").attr("action", "AjaxHandle/ExportExcel.ashx?type=game&plat=" + platname + "&time=" + time + "&impormcount=" + count + "&adomParameter=" + Math.floor(Math.random() * 1000 + 1));
              
                document.getElementById("ExcelForm").submit();

            }


    <form id="form1"><input id="Button1" onclick="game_Excel()" type="button" value="导出EXCEL" /></form>
    <form id="ExcelForm" style="display: none;" action="AjaxHandle/ExportExcel.ashx" method="post"></form>

    两个FORM,一个FORM添加一个导出按钮,另一个提交数据

    ExportExcel.ashx

                context.Response.ContentType = "text/plain";
                string strContent = "";
                 string strName = "";//文件名
                    string platType = context.Server.UrlDecode((context.Request["plat"] ?? string.Empty).ToString());//游戏平台
                    string platTime = context.Request.Params["time"];//时间
                    strName=platType + "_" + platTime;
                    DateTime dtime = DateTime.ParseExact(platTime, "yyyy-MM", System.Globalization.CultureInfo.CurrentCulture);//选中时间
                    string count = context.Request.Params["impormcount"];//时间
                    string where_str = string.Format(" and plat='{0}' and [time]='{1}' and importcount={2}", platType, dtime.ToString("yyyyMM"), count);
                    Model.GameData gd = new Model.GameData();
                    strContent = gd.GameTable(where_str);
                string officeType = "ToExcel";
             
                //string officeType = (context.Request["Type"] ?? string.Empty).ToString();
                //string strReportName = context.Server.UrlDecode((context.Request["Name"] ?? string.Empty).ToString());
                HttpResponse Response = context.Response;
                Response.ClearContent();
                Response.Charset = "UTF-8";
                Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(strName, System.Text.Encoding.UTF8).Replace("+", "%20")
                    + (officeType == "ToWord" ? ".doc" : ".xls"));
                Response.ContentType = officeType == "ToWord" ? "application/ms-word" : "application/excel";
                Response.Write("<html><head><title>" + strName + "</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>");
                //内容
                System.IO.StringWriter sw = new System.IO.StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                hw.Write(strContent);
                Response.Write(sw.ToString());
    
                Response.Write("</body></html>");
                Response.End();
    
  • 相关阅读:
    【JS】深拷贝与浅拷贝的区别,实现深拷贝的几种方法(讲的非常容易理解哦)
    关于splice()方法,slice() 、split()方法讲解,reverse()方法、replace()方法
    js面向对象高级编程
    js面向对象的几种设计模式,以及实现继承的几种方式
    解析Vue2.0和3.0的响应式原理和异同(带源码)
    总结js的几种数据类型检测方法
    javascript中Array常用方法,以及String的常用方法处理
    从输入URL到浏览器显示页面发生了什么,一个完整的http请求过程
    web前端如何防范安全攻击
    前端seo优化总结
  • 原文地址:https://www.cnblogs.com/zgaspnet/p/2487316.html
Copyright © 2011-2022 走看看