void Main()
{
DataTable dataTable =new DataTable();
for(int i=0;i<10;i++)
{
dataTable.Columns.Add("col"+i);
}
for(int i=0;i<1000000;i++)
{
var dr=dataTable.NewRow();
foreach(DataColumn col in dataTable.Columns)
{
dr[col]=col.ColumnName+i;
}
dataTable.Rows.Add(dr);
}
string fileName="excel.csv";
Stopwatch watch=new Stopwatch();
watch.Start();
ExportToExcel(dataTable,fileName);
watch.Stop();
("导出完毕,用时:"+watch.Elapsed).Dump();
}
public static void ExportToExcel(DataTable dataTable,string fileName,bool isOpen=false)
{
var lines = new List<string>();
string[] columnNames = dataTable.Columns
.Cast<DataColumn>()
.Select(column => column.ColumnName)
.ToArray();
var header = string.Join(",", columnNames);
lines.Add(header);
var valueLines = dataTable.AsEnumerable()
.Select(row => string.Join(",", row.ItemArray));
lines.AddRange(valueLines);
File.WriteAllLines(fileName,lines,Encoding.UTF8);
if(isOpen)
Process.Start(fileName);
}
---------------------
原文:https://blog.csdn.net/u012097590/article/details/78213452