求datagrid导出到txt的代码,请高手帮忙!谢谢!

解决方案 »

  1.   

    http://topic.csdn.net/t/20060221/16/4568625.html
      

  2.   

    上面的好象是导到excel,有没有导入txt的?
      

  3.   

    其实是一样的/// <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();
        }