C# .NET DataTable 转 CSV

DataTable 导出 CSV,支持.NET Framework 4.0以上。

public byte[] DataTableToCSV(DataTable data)
{
	StringBuilder sb = new StringBuilder();
	IEnumerable<string> columnNames = data.Columns.Cast<DataColumn>().
									  Select(column => column.ColumnName);
	sb.AppendLine(string.Join(",", columnNames));
	foreach (DataRow row in data.Rows)
	{
		IEnumerable<string> fields = row.ItemArray.Select(field =>
		  string.Concat("\"", field.ToString().Replace("\"", "\"\""), "\""));
		sb.AppendLine(string.Join(",", fields));
	}
	return System.Text.Encoding.Default.GetBytes(sb.ToString());
}
—— 完 ——
相关推荐
评论

立 为 非 似

中 谁 昨 此

宵 风 夜 星

。 露 , 辰

文章点击榜

细 无 轻 自

如 边 似 在

愁 丝 梦 飞

。 雨 , 花