我在 Windows Forms .NET 3.5 中有以下内容
它适用于记录少于 10,000 条记录的 csv,但对于超过 30,000 条记录的记录则较慢。输入 csv 文件可以包含 1 - 1,00,000 条记录之间的任何记录
当前使用的代码:
/// <summary>
/// This will import file to the collection object
/// </summary>
private bool ImportFile()
{
try
{
String fName;
String textLine = string.Empty;
String[] splitLine;
// clear the grid view
accountsDataGridView.Rows.Clear();
fName = openFileDialog1.FileName;
if (System.IO.File.Exists(fName))
{
System.IO.StreamReader objReader = new System.IO.StreamReader(fName);
do
{
textLine = objReader.ReadLine();
if (textLine != "")
{
splitLine = textLine.Split(',');
if (splitLine[0] != "" || splitLine[1] != "")
{
accountsDataGridView.Rows.Add(splitLine);
}
}
} while (objReader.Peek() != -1);
}
return true;
}
catch (Exception ex)
{
if (ex.Message.Contains("The process cannot access the file"))
{
MessageBox.Show("The file you are importing is open.", "Import Account", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
else
{
MessageBox.Show(ex.Message);
}
return false;
}
}
样本输入文件:
18906,Y
18908,Y
18909,Y
18910,Y
18912,N
18913,N
需要一些关于优化此代码以在网格中快速读取和查看的建议。