首页 > Web开发 > 详细

asp.net gridview 导出到excl

时间:2020-08-21 16:13:48      阅读:53      评论:0      收藏:0      [点我收藏+]
技术分享图片
  public void Export_EXCL(GridView GridView1)
    {
        /**
         * 如果打印全部数据,则加上注视的代码
         * */
        //GVExport.AllowPaging = false;
        //GVExport.AllowSorting = false;
        //GVExport.DataSource = null;
        //GVExport.DataBind();
        DateTime dt = DateTime.Now;
        Response.ClearContent();
        Response.Buffer = true;
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
        string filename = "export_" + dt.ToString("yyyyMMddHHmm") + ".xls";
        string[] browsers = { "Firefox", "AppleMAC-Safari", "Opera" }; //针对FF、Safari、Opera 设置编码
        string browser = Request.Browser.Browser;
        string attachment = string.Empty;
        if (Array.IndexOf<string>(browsers, browser) != -1)
        {
            attachment = "attachment; filename=" + filename;
        }
        else
        {
            attachment = "attachment; filename=" + Server.UrlEncode(filename);
        }
        Response.AddHeader("content-disposition", attachment);
        Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
        Response.ContentType = "application/ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.RenderControl(htw);
        Response.Output.Write(sw.ToString());
        Response.Flush();
        Response.End();
    }

    public override void VerifyRenderingInServerForm(Control control)
    {

    }
View Code

 

asp.net gridview 导出到excl

原文:https://www.cnblogs.com/tiancai/p/13541334.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!