qq_24504121 2015-09-29 15:42 采纳率: 50%
浏览 1678
已采纳

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

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

  • 写回答

1条回答 默认 最新

  • threenewbee 2015-09-29 15:45
    关注
     sw.Write(dt.Rows[i][j].ToString().Trim() + ",");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?