DesignCoding Studio

一切只要去努力!

定制颜色:
[ 显示模式: 摘要 | 列表 ]
2007-07-06

GridView导出Excel文件

时间: 2007-07-06 21:54:19 作者: admin 阅读次数: 1274
字体大小:

GridView导出Excel文件 

/// <summary>
/// 导出Excel文件
/// </summary>
/// <param name="ctl">要导出为Excel文件的控件</param>
/// <param name="FileName">文件名</param>

public static void ToExcel(System.Web.UI.Control ctl, string FileName)
{
      HttpContext.Current.Response.Charset
= "UTF-8";
      HttpContext.Current.Response.ContentEncoding
= System.Text.Encoding.UTF7;
      HttpContext.Current.Response.ContentType
= "application/ms-excel";
      HttpContext.Current.Response.AppendHeader(
"Content-Disposition", "attachment;filename=" + "" + FileName + ".xls");
      ctl.Page.EnableViewState
= false;
      System.IO.StringWriter tw
= new System.IO.StringWriter();
      HtmlTextWriter hw
= new HtmlTextWriter(tw);
      ctl.RenderControl(hw);
      HttpContext.Current.Response.Write(tw.ToString());
      HttpContext.Current.Response.End();
}
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: GridView Excel