Are there any CSV readers/writer libraries in C#?
5 Answers
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.
-
3Thanks, Josh. I gave this a go, and it was both fast and memory efficient. I think the documentation could be improved a bit for first-time users, but it was definitely sufficient.– SamDec 6, 2016 at 22:08
-
1Is 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).– u84sixOct 9, 2018 at 21:09
-
Yes. You can use
WriteField
. Check the docs here joshclose.github.io/CsvHelper Oct 10, 2018 at 1:06 -
1@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 Oct 4, 2019 at 15:55
-
@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 :-)– ZimanoOct 5, 2019 at 10:42
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.
-
6I always wondered why it was in the Microsoft.VisualBasic assembly... Did MS think that C# developers didn't use CSV ? Dec 21, 2009 at 17:36
-
4@Thomas: especially since a lot of C# developers cringe at the thought of including a VisualBasic assembly in their projects– user195488Dec 21, 2009 at 17:42
-
2Yeah - 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. Dec 21, 2009 at 17:44
-
21@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.– AshDec 30, 2009 at 2:18
-
3Another VB assembly gem is CopyDirectory: stackoverflow.com/questions/58744/…– user295190Dec 19, 2010 at 23:01
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();
}
};
}
}
-
2
-
2Writing CSV is easy - just use a normal text output method, and separate by commas. You really only need a library/custom handler for reading... Dec 21, 2009 at 17:26
-
11@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. Dec 21, 2009 at 18:03
-
28Writing 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.– TinisterDec 21, 2009 at 18:08
-
4
Yes - though I assume you're actually asking for specifics?
Try FileHelpers
-
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. Feb 4, 2011 at 9:02
-
1
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.
-
-
1I have found FileHelpers to be a nuisance to configure. FastCsvReader was a lot easier to use. Jan 19, 2010 at 16:12
-
+1 for FastCsvReader. Excellent performance. But it's a mainly a reader/parser. Not A writer. Nov 23, 2011 at 11:57
.net csv library
. Evidence, though anecdotal, for my claim is that this question has been viewed more than 22000 times in the past six years.