简体   繁体   English

如何有效地将大量数据从CSV文件导入DataGridView

[英]How to import large amounts of data from CSV file to DataGridView efficiently

I have 300 csv files that each file contain 18000 rows and 27 columns. 我有300个csv文件,每个文件包含18000行和27列。

Now, I want to make a windows form application which import them and show in a datagridview and do some mathematical operation later. 现在,我想创建一个Windows窗体应用程序,它导入它们并在datagridview中显示,然后再进行一些数学运算。

But, my performance is very inefficiently... 但是,我的表现非常低效......

After search this problem by google, I found a solution "A Fast CSV Reader". 通过谷歌搜索此问题后,我找到了一个解决方案“快速CSV阅读器”。 ( http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader ) http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader

I'm follow the code step by step, but my datagridview still empty. 我一步一步地遵循代码,但我的datagridview仍然是空的。

I don't know how to solve this problem. 我不知道如何解决这个问题。

Could anyone tell me how to do or give me another better way to read csv efficiently. 任何人都可以告诉我如何做或给我另一种更有效的方式来阅读csv。

Here is my code... 这是我的代码......

using System.IO;
using LumenWorks.Framework.IO.Csv;

private void Form1_Load(object sender, EventArgs e)
{
    ReadCsv();
}

void ReadCsv()
{
    // open the file "data.csv" which is a CSV file with headers
    using (CachedCsvReader csv = new
           CachedCsvReader(new StreamReader("data.csv"), true))
    {
        // Field headers will automatically be used as column names
        dataGridView1.DataSource = csv;
    }
}

Here is my input data: https://dl.dropboxusercontent.com/u/28540219/20130102.csv 这是我的输入数据: https//dl.dropboxusercontent.com/u/28540219/20130102.csv

Thanks... 谢谢...

You can also do like 你也可以这样做

  private void ReadCsv()
    {
        string filePath = @"C:\..\20130102.csv";
        FileStream fileStream = null;
        try
        {
            fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
        }
        catch (Exception ex)
        { 
            return;
        }

        DataTable table = new DataTable();
        bool isColumnCreated = false;
        using (StringReader reader = new StringReader(new StreamReader(fileStream, Encoding.Default).ReadToEnd()))
        {
            while (reader.Peek() != -1)
            {
                string line = reader.ReadLine();

                if (line == null || line.Length == 0)
                    continue; 

                string[] values = line.Split(',');

                if(!isColumnCreated)
                {
                    for(int i=0; i < values.Count(); i++)
                    {
                        table.Columns.Add("Column" + i);
                    }
                    isColumnCreated = true;
                }

                DataRow row = table.NewRow();

                for(int i=0; i < values.Count(); i++)
                {
                    row[i] = values[i];
                }
                table.Rows.Add(row);
            }
        }

        dataGridView1.DataSource = table;
    }

在此输入图像描述

Based on you performance requirement, this code can be improvised. 根据您的性能要求,此代码可以即兴创作。 It is just a working sample for your reference. 这只是一个工作样本供您参考。

I hope this will give some idea. 我希望这会给出一些想法。

The data you provide contains no headers (first line is a data line). 您提供的数据不包含标题(第一行是数据行)。 So I got an ArgumentException (item with same key added) when I tried to add the csv reader to the DataSource. 所以当我尝试将csv阅读器添加到DataSource时,我得到了一个ArgumentException(添加了相同键的项)。 Setting the hasHeaders parameter in the CachCsvReader constructor did the trick and it added the data to the DataGridView (very fast). 在CachCsvReader构造函数中设置hasHeaders参数可以解决问题,并将数据添加到DataGridView(非常快)。

using (CachedCsvReader csv = new CachedCsvReader(new StreamReader("data.csv"), false))
{
     dataGridView.DataSource = csv;
}

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM