zoukankan      html  css  js  c++  java
  • .net中从GridView中导出数据到excel(详细)

    1,创建数据源 找到要导出的GridView中的数据。

    2,重写VerifyRenderingInServerForm方法。

       public override void VerifyRenderingInServerForm(Control control)
     {

        }

    3,编写导到Excel的方法。

    private void ExportGridView()
            {
                /**
                 * 如果打印全部数据,则加上注视的代码
                 * */
                //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 = "XX_" + 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);
                GVExport.RenderControl(htw);

                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }

  • 相关阅读:
    HDU 4489 The King’s Ups and Downs (DP+数学计数)
    HDU 2084 数塔 (水DP)
    CodeForces 706C Hard problem (水DP)
    CodeForces 706B Interesting drink (二分查找)
    CodeForces 706A Beru-taxi (数学计算,水题)
    HDU 4438 Hunters (数学,概率计算)
    机器学习-基础知识
    如何学习-学习理论(A-1)-KOLB学习周期理论
    如何学习-辅助工具
    如何学习-入门之后
  • 原文地址:https://www.cnblogs.com/lintao0823/p/3318568.html
Copyright © 2011-2022 走看看