Best way to parse csv file?
I have a csv file that contains 10 columns. I specifically need to use 2 of those columns: reportid and amount. I could have multiple lines for each report id. I'd like to get the sum for each report id. What is the best way to accomplish this using c#? Thanks.
Abhishek KumarPosted Oct 11, 2014, 11:22 AM
Follow the article below,
http://www.codeproject.com/Articles/667317/Read-CSV-file-using-Csharp
Steps:
Read the file -> Fill the dataset -> manupulate according your need ->Display the Total for each report ID
Hope this will help , if you need sample code or solution please share the csv file.
Abhishek
Wim SturkenboomPosted Oct 11, 2014, 6:45 AM
The above supports all csv goodies like multiline fields, embedded quotes and embedded commas etc; not all parsers on the web do so. I've modified it for my use so it can handle different separators as well (standard is comma only).
Alternative for the reader can be the 'standard' TextFieldParser class; to use it, you need to add a reference to Microsoft.VisualBasic to your project and to add using Microsoft.VisualBasic.FileIO to the files where you want to use it.
You can paste below text into a text file and call it whatever.csv. If the reader parses it correctly, you have a good parser. Note that it contains only two records and the last column of the first record contains two lines. Also, the first record contains a column with an embedded comma.
record 1,"a,b","c""d","line1
line 2"
record 2,x\y\z,x/y/z,empty ;)
Hope this helps.