C# 将数据表中的行写入文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11749812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Write Rows from DataTable to Text File
提问by RJ.
public void GenerateDetailFile()
{
if (!Directory.Exists(AppVars.IntegrationFilesLocation))
{
Directory.CreateDirectory(AppVars.IntegrationFilesLocation);
}
DateTime DateTime = DateTime.Now;
using (StreamWriter sw = File.CreateText(AppVars.IntegrationFilesLocation +
DateTime.ToString(DateFormat) + " Detail.txt"))
{
DataTable table = Database.GetDetailTXTFileData();
foreach (DataRow row in table.Rows)
{
sw.WriteLine(row);
}
}
}
Not sure what I'm missing here but I think it might be the column name which I'm not sure how to set it up.
不确定我在这里遗漏了什么,但我认为这可能是我不确定如何设置的列名。
This is working fine, except, when it writes to the text file, it's writing this:
这工作正常,除了当它写入文本文件时,它是这样写的:
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
System.Data.DataRow
Can anyone give me a hand?
谁能帮我一把?
采纳答案by Michael
Something like:
就像是:
sw.WriteLine(row["columnname"].ToString());
would be more appropriate.
会更合适。
回答by mellamokb
There is no "natural" string representation for a DataRow. You need to write it out in whatever format you desire, i.e., comma-separated list of values, etc. You can enumerate the columns and print their values, for instance:
DataRow 没有“自然”字符串表示。您需要以您想要的任何格式写出它,即以逗号分隔的值列表等。您可以枚举列并打印它们的值,例如:
foreach (DataRow row in table.Rows)
{
bool firstCol = true;
foreach (DataColumn col in table.Columns)
{
if (!firstCol) sw.Write(", ");
sw.Write(row[col].ToString());
firstCol = false;
}
sw.WriteLine();
}
回答by HatSoft
You need to write the columns from each DataRow. Currently you are writing the DataRow object that is dataRow.ToString() hence you get string name "System.Data.DataRow"of dataRow in your file
您需要从每个 DataRow 中写入列。当前您正在编写 DataRow 对象,即 dataRow.ToString() 因此您"System.Data.DataRow"在文件中获得dataRow 的字符串名称
foreach(DataRow row in table.Rows)
{
foreach(DataColumn column in table.Columns)
{
sw.WriteLine(row[column]);
}
}
回答by Eric
When you try to print out a DataRowlike that, it is calling Object.ToString(), which simply prints out the name of the type. What you want to do is something like:
当您尝试打印出DataRow类似的a 时,它正在调用Object.ToString(),它只是打印出类型的名称。你想要做的是:
sw.WriteLine(String.Join(",", row.ItemArray));
This will print a comma separated list of all of the items in the DataRow.
这将打印 .csv 文件中所有项目的逗号分隔列表DataRow。
回答by Lawrine
The below code will let you to write text file each column separated by '|'
下面的代码将让您编写文本文件,每列以“|”分隔
foreach (DataRow row in dt.Rows)
{
object[] array = row.ItemArray;
for (int i = 0; i < array.Length - 1; i++)
{
swExtLogFile.Write(array[i].ToString() + " | ");
}
swExtLogFile.WriteLine(array[array.Length - 1].ToString());
}
回答by Maghalakshmi Saravana
Try this:
尝试这个:
To write the DataTable rows to text files in the specific directory
将 DataTable 行写入特定目录中的文本文件
var dir = @"D:\New folder\log"; // folder location
if (!Directory.Exists(dir)) // if it doesn't exist, create
Directory.CreateDirectory(dir);
foreach (DataRow row in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
result.Append(row[i].ToString());
result.Append(i == dt.Columns.Count - 1 ? "\n" : ",");
}
result.AppendLine();
}
string path = System.IO.Path.Combine(dir, "item.txt");
StreamWriter objWriter = new StreamWriter(path, false);
objWriter.WriteLine(result.ToString());
objWriter.Close();

