Parse a CSV file in C#?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a CSV file with some data (7,500 rows , 18 columns)

How can I parse this file in C#?

Can I programmatically save the file as an Excel Workbook and then parse the
file using the Excel Namespace Methods available?

Or can I programmatically save the CSV file as an XML file and then parse
the XML?

Any advice is much appreciated.

Thanks.
 
Open with a StreamReader, read each line, then do a String.Split on the
commas. Repeat for each line.
 
Open with a StreamReader, read each line, then do a String.Split on the
commas. Repeat for each line.

That doesn't work for all CSV files, though. The data in a data row may
contain line breaks, so a data row may span over any number of lines.

Fields that contain line breaks are generally enclosed in quotes. A quoted
field may also contain double quotes to represent quotes in the data.

Sadly there exists no independent standard for the CSV file format. Most CSV
files are not even comma separated at all, but semicolon separated.

The format that Microsoft Office uses seems to be a de-facto standard, but
as it's not a real standard, your CSV files may actually look rather
differently.
 
Back
Top