更快的方式读取csv到网格 [英] Faster way of reading csv to grid

查看:179
本文介绍了更快的方式读取csv到网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows Forms .NET 3.5中有以下操作



对于记录小于10,000的csv,它可以正常工作,但对于超过30,000的记录,它可以更慢。
输入csv文件可以记录1 - 1,00,000条记录之间的任何记录



当前使用的代码:

  ///< summary> 
///这将导入文件到集合对象
///< / summary>
private bool ImportFile()
{
try
{

String fName;
String textLine = string.Empty;
String [] splitLine;

//清除网格视图

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(进程无法访问该文件))
{
MessageBox.Show(正在导入的文件已打开。,导入帐户,MessageBoxButtons.OK,MessageBoxIcon.Warning);
}
else
{
MessageBox.Show(ex.Message);
}

return false;
}

}

示例输入文件:


18906,Y

18908,Y

18909,Y

18910,Y

18912,N

18913,N



需要一些关于优化此代码以便快速读取的建议。

解决方案

 列表< string []> rows = File.ReadAllLines(Path)。选择(x => x.Split(','))ToList(); 
DataTable dt = new DataTable();
dt.Columns.Add(1);
dt.Columns.Add(2);
rows.ForEach(x => {
dt.Rows.Add(x);
});
dgv.DataSource = dt;

尝试一下,我怀疑你在datagrid中有一些形式的列名,

 <$ c按照原始代码过滤

$ c> List< string []> rows = File.ReadAllines(Path)。选择(x => x.Split(','))where(x => x [0]!=&& x [1]! =).ToList();

要从 DataGridView / p>

  dt.Columns.AddRange(dgv.Columns.Cast< DataGridViewColumn>()。Select(x => new DataColumn .Name))。ToArray()); 


I have following in Windows Forms .NET 3.5

It works fine for csv with records less than 10,000 but is slower for records above 30,000. Input csv file can can any records between 1 - 1,00,000 records

Code currently used :

/// <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;
            }

        }

Sample Input file :
18906,Y
18908,Y
18909,Y
18910,Y
18912,N
18913,N

Need some advice on optimizing this code for fast reads & view in grid.

解决方案

List<string[]> rows = File.ReadAllLines("Path").Select(x => x.Split(',')).ToList();
DataTable dt = new DataTable();
dt.Columns.Add("1");
dt.Columns.Add("2");
rows.ForEach(x => {
  dt.Rows.Add(x);
});
dgv.DataSource = dt;

Try that, I suspected that you have some form of column names in the datagrid for now I just made them 1 and 2.

To filter as per your original code use:

List<string[]> rows = File.ReadAllines("Path").Select(x => x.Split(',')).Where(x => x[0] != "" && x[1] != "").ToList();

To get your columns from the DataGridView

  dt.Columns.AddRange(dgv.Columns.Cast<DataGridViewColumn>().Select(x => new DataColumn(x.Name)).ToArray());

这篇关于更快的方式读取csv到网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆