How do I read large CSV files using C# & display it on the browser?
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.
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.
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'.
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.
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
Use http://csvhelper.com to read it and bind the data to a GridView.