首页 新闻 会员 周边

.net dataTable导出txt问题,在线等。

0
悬赏园豆:5 [已解决问题] 解决于 2015-09-30 09:44

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文件,一定是我循环的不对。
图片说明
在线等,求解。

奈何桥下的主页 奈何桥下 | 初学一级 | 园豆:3
提问于:2015-09-30 09:23
< >
分享
最佳答案
0
sw.Write(dt.Rows[i][j].ToString().Trim() + ",");

应该是+ ","

收获园豆:5
请叫我头头哥 | 大侠五级 |园豆:9382 | 2015-09-30 09:44
其他回答(1)
0

那你用什么WriteLine?用Write,sw.Write(dt.Rows[i][j].ToString().Trim()+"    ");  

稳稳的河 | 园豆:4216 (老鸟四级) | 2015-09-30 09:38
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册