141

Are there any CSV readers/writer libraries in C#?

0

5 Answers 5

124

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 ); 

Full Disclosure: I am the author of this library.

Sign up to request clarification or add additional context in comments.

5 Comments

Is there a way to use this library without a custom type so that I can just iterate through my table grid (custom) and write out header and then pass each row field? I'm just looking for a way to prevent errors in the files (escaping properly etc).
Yes. You can use WriteField. Check the docs here joshclose.github.io/CsvHelper
@Zimano Not true. You can read individual fields or even just get a string array for the row. Check out the documentation. joshclose.github.io/CsvHelper
@JoshClose Oh, that's good to hear, thanks! I only checked out the getting started guide here: joshclose.github.io/CsvHelper/… and it didn't include my use case. I should've looked further, my apologies. However, on that page it does say that using a mapping of some kind is the recommended way to use CsvHelper. Perhaps it could offer some alternative read strategies on that spot? I've deleted my original remark by the way, as it's now irrelevant :-)
Sure. Submit an issue on GitHub to add it to the documentation and it'll get added
23

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.

7 Comments

I always wondered why it was in the Microsoft.VisualBasic assembly... Did MS think that C# developers didn't use CSV ?
@Thomas: especially since a lot of C# developers cringe at the thought of including a VisualBasic assembly in their projects
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.
@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.
Another VB assembly gem is CopyDirectory: stackoverflow.com/questions/58744/…
|
19

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 a file:

private void exportDGVToCSV(string filename) { if (dataGridView1.Columns.Count != 0) { using (Stream stream = File.OpenWrite(filename)) { stream.SetLength(0); using (StreamWriter writer = new StreamWriter(stream)) { // loop through each row of our DataGridView foreach (DataGridViewRow row in dataGridView1.Rows) { string line = string.Join(",", row.Cells.Select(x => $"{x}")); writer.WriteLine(line); } writer.Flush(); } }; } } 

9 Comments

but can't it write also?
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: 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.
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.
The standardization is RFC 4180. tools.ietf.org/html/rfc4180
|
7

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

Try FileHelpers

2 Comments

Well, I did but for some strange reason FileHelpers broke down randomly. Furthermore, the FileHelpers library has not been in development for a long time.
FileHelpers are actively developed now, see the GitHub.
4

There are dozens.

http://www.filehelpers.net/ 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 don't know the format of your CSV file or import mapping until runtime - this is the better library to use.

3 Comments

We use filehelpers also.
I have found FileHelpers to be a nuisance to configure. FastCsvReader was a lot easier to use.
+1 for FastCsvReader. Excellent performance. But it's a mainly a reader/parser. Not A writer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.