把数据写入到Text中
public static string TableToTextFile(DataTable dt,string FullFileName)
{
string fp=FullFileName;
try
{
using (StreamWriter sw = new StreamWriter(fp))
{
DataRow[] myRow=dt.Select();
//取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
string title="";
int i;
for(i=0;i<dt.Columns.Count;i++)
{
title+=dt.Columns[i].Caption.ToString()+"\t";
}
sw.WriteLine(title);
string line="";
foreach(DataRow row in myRow)
{
//在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
line="";
for(i=0;i<row.ItemArray.Length;i++)
{
line+=row[i].ToString()+"\t";
}
sw.WriteLine(line);
}
sw.Close();
}
}
catch(Exception ex)
{
fp="0||"+ex.Message;
}
return fp;
}
public static string TableToTextFile(DataTable dt,string FullFileName)
{
string fp=FullFileName;
try
{
using (StreamWriter sw = new StreamWriter(fp))
{
DataRow[] myRow=dt.Select();
//取得数据表各列标题,各标题之间以\t分割,最后一个列标题后加回车符
string title="";
int i;
for(i=0;i<dt.Columns.Count;i++)
{
title+=dt.Columns[i].Caption.ToString()+"\t";
}
sw.WriteLine(title);
string line="";
foreach(DataRow row in myRow)
{
//在当前行中,逐列获得数据,数据之间以\t分割,结束时加回车符\n
line="";
for(i=0;i<row.ItemArray.Length;i++)
{
line+=row[i].ToString()+"\t";
}
sw.WriteLine(line);
}
sw.Close();
}
}
catch(Exception ex)
{
fp="0||"+ex.Message;
}
return fp;
}
本文介绍了一种将DataSet中的DataTabel数据转换为文本文件的方法。通过使用StreamWriter,可以将表格数据按列标题和数据行的形式输出到指定路径的文本文件中。此方法适用于需要将数据库或内存中的数据导出为简单文本格式的场景。

被折叠的 条评论
为什么被折叠?



