string path = ‘path’;
StringBuilder builder = new StringBuilder();
builder.AppendLine(errorTable.Columns[0].ColumnName + '/' + errorTable.Columns[1].ColumnName + '/' + errorTable.Columns[2].ColumnName + '/' + errorTable.Columns[3].ColumnName);
foreach (DataRow dr in errorTable.Rows)
{
builder.AppendLine(dr[0].ToString() + ',' + dr[1].ToString() + ',' + dr[2].ToString() + ',' + dr[3].ToString());
using (System.IO.FileStream file = new System.IO.FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
using (System.IO.TextWriter text = new System.IO.StreamWriter(file, System.Text.Encoding.Default))
{
text.Write(builder.ToString());
}
}
}
本文展示了一段使用C#语言将DataTable中的数据导出到CSV文件的代码示例。首先定义了文件路径并创建了一个StringBuilder对象来拼接CSV内容。接着通过遍历DataTable的列名和行数据来构建CSV文件的内容,并最终将StringBuilder中累积的数据写入指定路径的文件中。
1530

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



