tags:

views:

863

answers:

6

are there any CSV readers/writer lib's in c#

+4  A: 

Yes - though I assume you're actually asking for specifics?

Try FileHelpers

Murph
+2  A: 

There are dozens.

http://www.filehelpers.com/ is one of the most common.

I should say that I find Filehelpers restrictive in some scenarios, and instead use The Fast CSV Reader. In my experience, if you dont know the format of your CSV file or import mapping until runtime - this is the better library to use.

Alex
We use filehelpers also.
Tangurena
I have found FileHelpers to be a nuisance to configure. FastCsvReader was a lot easier to use.
Josh Close
+5  A: 

Sebastien Lorion has a great CSV reader on CodeProject called A Fast CSV Reader. Probably one of the best for C# and it's free.

As far as writing, just use StreamWriter!

Here is some boilerplate code for writing a DataGridView to file:

private void exportDGVToCSV(string filename)
    {
        if(dataGridView1.Columns.Count != 0) {

            StringBuilder myString = new StringBuilder();

            if (File.Exists(FilePath + "file"))
            {
                File.Delete(FilePath + "file");
            }

            StreamWriter sw = new StreamWriter(filename, true);

            // loop through each row of our DataGridView

            foreach(DataGridViewRow row in dataGridView1.Rows) {
                foreach(DataGridViewCell cell in row.Cells) {               
                    myString.Append(cell.Value + ",");
                    myString.Append("\r\n");

                }
            }
            sw.WriteLine(myString.ToString());
            sw.Close();

            GC.Collect();

        }
    }
0A0D
This is my favourite. Great little library.
Alex
but can't it write also?
mrblah
I know - I have used it for the past two years - no issues!
0A0D
Writing CSV is easy - just use a normal text output method, and separate by commas. You really only need a library/custom handler for reading...
Reed Copsey
@Reed: it is not too trivial if you want to have commas and quotation marks escaped correctly. I'm not even sure if there is a standardization for this, but is is very common to do it.
Stefan Steinegger
Writing CSV is a bit more complex than that. If the data you want expressed in the CSV file contains commas or new lines, you need to surround the data with quotation marks. If there's a quotation mark in the newly quotation-marked data, then you need to escape the quotation mark with double quotation marks. You could certainly code that logic yourself but it would be nice for a library to do it for you.
Tinister
@Tinister: Sure, it was a simple example - but where's the standardization?
0A0D
+8  A: 

There are a couple of options, right in the framework itself.

One of the easiest is to Reference Microsoft.VisualBasic, then use TextFieldParser. It is a fully functional CSV reader in the core framework.

Another good alternative is to use datasets to read CSV files.

Reed Copsey
I always wondered why it was in the Microsoft.VisualBasic assembly... Did MS think that C# developers didn't use CSV ?
Thomas Levesque
@Thomas: especially since a lot of C# developers cringe at the thought of including a VisualBasic assembly in their projects
0A0D
Yeah - there are lots of nice "goodies" in there. I think it's more because VB has some language constructs that weren't really considered in the framework originally, but they would never have been able to get VB6 users across without implementing them. The Devices and ApplicationServices namespaces have all sorts of useful things.
Reed Copsey
@Roboto,any C# developer that cringes at referencing Microsoft.VisualBasic in their project is an ignorant language snob. Even worse they don't understand .NET at all.
Ash
+2  A: 

Try CsvHelper. It's as easy to use as FastCsvReader and does writing also. I've been very happy with FastCsvReader in the past, but I needed something that does writing also, and wasn't happy with FileHelpers.

Reading:

var csv = new CsvReader( stream );
var myCustomTypeList = csv.GetRecords<MyCustomType>();

Writing:

var csv = new CsvWriter( stream );
csv.WriteRecords( myCustomTypeList );
Josh Close