Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
Hi All,
I need to open a CSV file, what is the best way of doing this?
C#
var reader = new StreamReader(File.OpenRead(@"C:\MeterDataCSV.CSV"));

This I am sure opens the CSV. Now I have it open for reading how can I get the data out, I am sure that a assigning reader to a text box or a rich text box? can I...

I have found:
A Fast CSV Reader[^]
Posted
Updated 17-Apr-14 4:36am
v2

I use this handy method to parse a .csv file:

Here is where I call the method and then iterate the data:
List<string[]> data = new List<string[]>();
data = parseCSV(path);

foreach (string[] item in data)
{
}


"path" is the location of your file.

public List<string[]> parseCSV(string path)
   {
       List<string[]> parsedData = new List<string[]>();
       try
       {
           using (StreamReader readFile = new StreamReader(path))
           {
               string line;
               string[] row;

               while ((line = readFile.ReadLine()) != null)
               {
                   row = line.Split(',');
                   parsedData.Add(row);
               }
           }
       }
       catch (Exception e)
       {
           MessageBox.Show(e.Message);
       }

       return parsedData;
   }
 
Share this answer
 
v2
Comments
glennPattonWork3 17-Apr-14 10:40am    
Thanks... I was in the process of modding the question as your reply landed!
I need to set readFile don't I....
Richard C Bishop 17-Apr-14 10:44am    
readFile in my code is comparable to reader in your code. You would not need that line of code you have if you use my method. See my updated solution to see how I use it.
glennPattonWork3 17-Apr-14 10:47am    
Thanks. Have a 5 on me!!
Richard C Bishop 17-Apr-14 10:49am    
You are welcome, and thank you. Let me know if you have any issues.
glennPattonWork3 17-Apr-14 11:14am    
I have managed to short circuit permissions (finally). It appears I can see the file, the data is there but how can I output it as a messagebox for instance.
i don't think textbox or richtextbox suitable for display csv file table like data


you can use Fast CSV reader and bind data directly to a data grid like below

C#
using System.IO;
using LumenWorks.Framework.IO.Csv;
void ReadCsv()
{
    // open the file "data.csv" which is a CSV file with headers
    using (CachedCsvReader csv = new
           CachedCsvReader(new StreamReader(@"C:\MeterDataCSV.CSV"), true))
    {
        // Field headers will automatically be used as column names
        myDataGrid.DataSource = csv;
    }
}
 
Share this answer
 
v2
Comments
glennPattonWork3 17-Apr-14 11:04am    
I am more than a little confused by the using LumenWorks.IO.Csv.... need to included it but how?
DamithSL 17-Apr-14 11:36am    
go to http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader and you can Download binaries from the link given on top of that page. browse and add reference to umenWorks.IO.Csv.dll

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900