当前位置: 代码迷 >> ASP.NET >> 求datagrid导出到txt的代码解决方案
  详细解决方案

求datagrid导出到txt的代码解决方案

热度:1343   发布时间:2013-02-25 00:00:00.0
求datagrid导出到txt的代码
求datagrid导出到txt的代码,请高手帮忙!谢谢!

------解决方案--------------------------------------------------------
http://topic.csdn.net/t/20060221/16/4568625
------解决方案--------------------------------------------------------
其实是一样的

/// <summary>
/// 导出为 Excel 函数
/// </summary>
/// <param name= "ctl "> </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.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel "; //response.contenttype指定文件类型 可以为application/ms-excel、application/ms-word、application/ms-txt、application/ms-html 或其他浏览器可直接支持文档
HttpContext.Current.Response.AppendHeader( "Content-Disposition ", "attachment;filename= " + " " + FileName + ".xls "); //filename=fileflow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc    .xls    .txt   .htm
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();
}
  相关解决方案