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();
}
sw.Write(dt.Rows[i][j].ToString().Trim() + ",");
应该是+ ","
那你用什么WriteLine?用Write,
sw.Write(dt.Rows[i][j].ToString().Trim()+" ");