C# 倒出Excel表格

#region 倒出Excel表格
  
/// <summary>
  /// 倒出Excel表格
  /// </summary>
  /// <param name="ctl"></param>

  public void ToExcel(System.Web.UI.Control ctl)

 {
      HttpContext.Current.Response.Charset = "GB2312";
      HttpContext.Current.Response.AppendHeader("Content-    Disposition", "attachment;filename=" +System.DateTime.Now.ToString("yyyyMMddhhmmss") + "_" +HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes("表格")) + ".xls");
      HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
      HttpContext.Current.Response.ContentType = "application/ms-execl";
      ctl.Page.EnableViewState = false;
      System.IO.StringWriter tw= new System.IO.StringWriter();
      System.Web.UI.HtmlTextWriter hw = new     System.Web.UI.HtmlTextWriter(tw);
      ctl.RenderControl(hw);
      HttpContext.Current.Response.Write(tw.ToString());
      HttpContext.Current.Response.End();
  }
  #endregion 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章