dataTable导出txt 一下是我的代码
private static void ExportTxt(DataTable dt, string fileName)
{
HttpContext.Current.Response.Clear();
string FileName = fileName + ".txt";
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
HttpContext.Current.Response.ContentType = "text/plain";
System.IO.StringWriter sw = new System.IO.StringWriter();
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
sw.WriteLine(dt.Rows[i][j].ToString().Trim());
}
sw.WriteLine();
}
HttpContext.Current.Response.Write(sw.ToString());
sw.Close();
HttpContext.Current.Response.End();
}
结果成这样
而我需要这样的形式的txt文件,一定是我循环的不对。
在线等,求解。