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文件,一定是我循環的不對。
在線等,求解。
sw.Write(dt.Rows[i][j].ToString().Trim() + ",");