【发布时间】:2012-09-01 01:35:55
【问题描述】:
我需要将大的DataTable(> 50 lacs(5M) DataRows) 导出到.csv 文件
我正在使用下面的代码,但它需要很长时间。
public void CreateCSVFile(DataTable dtDataTablesList, string strFilePath)
{
// Create the CSV file to which grid data will be exported.
StreamWriter sw = new StreamWriter(strFilePath, false);
//First we will write the headers.
int iColCount = dtDataTablesList.Columns.Count;
for (int i = 0; i < iColCount; i++)
{
sw.Write(dtDataTablesList.Columns[i]);
if (i < iColCount - 1)
{
sw.Write("", "");
}
}
sw.Write(sw.NewLine);
// Now write all the rows.
foreach (DataRow dr in dtDataTablesList.Rows)
{
for (int i = 0; i < iColCount; i++)
{
if (!Convert.IsDBNull(dr[i]))
{
sw.Write(dr[i].ToString());
}
if (i < iColCount - 1)
{
sw.Write("", "");
}
}
sw.Write(sw.NewLine);
}
sw.Close();
}
请告诉我任何其他快速完成的方法。
【问题讨论】:
-
@JohnWoo 他要求从 DB 中以 CSV 格式写入数据.. 上面应该可以吗?我相信它是 CSV 阅读器...
-
系统挂起,我无法测量这里的时间,但 100 万条记录插入 2 分钟。