tags:

views:

1803

answers:

7

How do I read large CSV files using C# & display it on the browser?

+1  A: 

You might be interested in Linq2Csv library at CodeProject. One thing you would need to check is that if it's reading the data when it needs only, so you won't need a lot of memory when working with bigger files.

As for displaying the data on the browser, you could do many things to accomplish it, if you would be more specific on what are your requirements, answer could be more specific, but things you could do:
1. Use HttpListener class to write simple web server (you can find many samples on net to host mini-http server).
2. Use Asp.Net or Asp.Net Mvc, create a page, host it using IIS.

Ravadre
+2  A: 

I just used this library in my application. http://www.codeproject.com/KB/database/CsvReader.aspx. Everything went smoothly using this library, so I'm recommending it. It is free under the MIT License, so just include the notice with your source files.

I didn't display the CSV in a browser, but the author has some samples for Repeaters or DataGrids. I did run one of his test projects to test a Sort operation I have added and it looked pretty good.

benjynito
+5  A: 

Two projects of interest:

http://filehelpers.sourceforge.net/

http://www.codeproject.com/KB/database/CsvReader.aspx

Oliver
The Lumenworks CSV library is definitely the best free C#/.NET CSV framework out there.
Chris S
FileHelpers is pretty sweet. Use it for delimited or fixed length files. Can also use it with mixed format and mixed length. Have used it in multiple projects with no problems.
mattsmith321
A: 

This is just for parsing the CSV, for displaying it in a web page, you just simply tak the list and do render it howevr you want.


public List SplitCSV(string line)
        {
            if (string.IsNullOrEmpty(line))
                throw new ArgumentException();

            List result = new List();

            int index = 0;
            int start = 0;
            bool inQuote = false;
            StringBuilder val = new StringBuilder();

            // parse line
            foreach (char c in line)
            {
                switch (c)
                {
                    case '"':
                        inQuote = !inQuote;
                        break;
                    case ',':
                        if (!inQuote)
                        {
                            result.Add(line.Substring(start, index - start).Replace("\"",""));
                            start = index + 1;
                        }
                        break;
                }
                index++;
            }
            if (start < index)
            {
                result.Add(line.Substring(start, index - start).Replace("\"",""));
            }
            return result;
        }
    }

This code does not handles \n inside 'lines'.

Freddy
It also doesn't handle the use of double-quote characters within a value. CSV has many edge cases, it is best to just use a library.
Arafangion
+1  A: 

Seems like there are quite a few projects on CodeProject or CodePlex for CSV Parsing. Here is another CSV Parser on CodePlex

http://commonlibrarynet.codeplex.com/

This library has components for CSV parsing, INI file parsing, Command-Line parsing as well. It's working well for me so far. Only thing is it doesn't have a CSV Writer.

alex
A: 

Geez, the simple way:

Create a DataTable object, add the columns you expect your data to parse into,

Read your data line by line, split the line into an array and insert it into the DataTable object

Put a GridView object on your page and set the DataTable containing your data as the GridView.DataSource for the grid.

bam done.

Adjust the properties of the GridView as desired.

Ron

Ron Savage
-1, because the hard part of CSV parsing is in "split the line into an array"
Roger Lipscombe
A: 

Use http://csvhelper.com to read it and bind the data to a GridView.

Josh Close