如何把GridView导入execl中去

解决方案 »

  1.   

    public void ToExcel(System.Web.UI.WebControls.GridView  gw, string filename)
            {
                System.Web.HttpContext.Current.Response.Clear();
                System.Web.HttpContext.Current.Response.AddHeader("content-disposition",
                "attachment;filename=FileName.xls");
                System.Web.HttpContext.Current.Response.Charset = "";
                // If you want the option to open the Excel file without saving than
                // comment out the line below
                //Response.Cache.SetCacheability(HttpCacheability.NoCache);
                System.Web.HttpContext.Current.Response.ContentType = "application/" + filename;
                System.IO.StringWriter stringWrite = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
                
                gw.RenderControl(htmlWrite);
                System.Web.HttpContext.Current.Response.Write(stringWrite.ToString());
                System.Web.HttpContext.Current.Response.End();
                
            }
      

  2.   

    http://jackyrong.cnblogs.com/archive/2006/01/21/321198.htmlhttp://aspalliance.com/771
      

  3.   

    public static void ExportToExcel(string Filename, GridView gridview,Page page) 

    page.Response.Clear(); 
    // 防止中文内容为乱码 
    page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); 
    //可令中文文件名不为乱码 
    page.Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(Filename + DateTime.Now.ToShortDateString(), System.Text.Encoding.UTF8) + ".xls\""); 
    StringWriter sw = new StringWriter(); 
    HtmlTextWriter htw = new HtmlTextWriter(sw); 
    gridview.RenderControl(htw); 
    page.Response.Write(sw.ToString()); 
    page.Response.End(); 
    } 调用方法:类.ExportToExcel("中文文件名",GridView1,this.Page);