close

GridView gvExport = new GridView();
  gvExport.DataSource = getDataTable(strQuery);
  gvExport.DataBind();

  string strExportFilename = "ExportedData.xls"; 

  Response.Clear();
  Response.AddHeader("content-disposition", 
       "attachment;filename=" + strExportFilename + ".xls");
  Response.Cache.SetCacheability(HttpCacheability.NoCache);
  Response.ContentType = "application/vnd.xls";
  Response.Charset = "big5";
  
  System.IO.StringWriter stringWrite = new System.IO.StringWriter();
  System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
  gvExport.RenderControl(htmlWrite);
  Response.Write(stringWrite.ToString().Replace("<div>", "").Replace("</div>", ""));
  Response.End();

EnableEventValidation = "false" 

 

 public override void VerifyRenderingInServerForm(Control control)
 {
     //
處理'GridView' 的控制項 'GridView' 必須置於有 runat=server 的表單標記之中
 }

http://blog.miniasp.com/post/2008/02/12/Export-data-to-Excel-with-ease-using-ASPNET.aspx

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 amychang2014 的頭像
    amychang2014

    工作需要筆記

    amychang2014 發表在 痞客邦 留言(0) 人氣()