我想在for循环中写一个DataTable
但是,写入步骤似乎打破了for循环。 for循环只运行一次。
如何在不中断for循环的情况下从.csv
编写DataTable
文件?
for (i=0;i<10;i++)
{
StringBuilder sb = new StringBuilder();
IEnumerable<string> columnNames = listDataTable[dtIndex].Columns.Cast<DataColumn>().
Select(column => column.ColumnName);
sb.AppendLine(string.Join(",", columnNames));
foreach (DataRow row in listDataTable[dtIndex].Rows)
{
IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
sb.AppendLine(string.Join(",", fields));
}
File.WriteAllText("test.csv", sb.ToString());
}