C#导出到txt文件的三种方法

方法一: 

HttpResponse resp = System.Web.HttpContext.Current.Response;
resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
resp.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(FileName));
resp.Write(sb.ToString());

 

方法二: 

Console.WriteLine(sb.ToString());
FileStream aFile = new FileStream("D:\\temp\\lintest.dat", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile);
sw.Write(sb.ToString());
sw.Close();

 

方法三: 

Response.Clear();
Response.Buffer = true;
Response.ContentType = "text/richtext";
Response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8) + ";size=0");
Response.Write(sb.ToString());
Response.Flush();
Response.End();

你可能感兴趣的:(C#)