Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing a CSV file in .net

Tags:

c#

.net

csv

I have a requirement to export a dataset as a CSV file.

I have spent a while searching for a set of rules to go by and realised there are quite a few rules and exceptions when writing a CSV file.

http://knab.ws/blog/index.php?/archives/3-CSV-file-parser-and-writer-in-C-Part-1.html http://bytes.com/topic/c-sharp/answers/236875-problems-streamwriter-output-csv http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/0073fcbb-adab-40f0-b768-4bba803d3ccd

So now it is not a simple process of separating strings with commas, I have searched for an existing CSV writer either 3rd party or (hopefully!) included in the .net framework.

Edit: New link: http://www.thinqlinq.com/Post.aspx/Title/LINQ-to-CSV-using-DynamicObject-and-TextFieldParser

The TextFieldParser is a VB object (can be referenced from C#) that will automatically parse CSV files. :)

I was wondering if anybody knows any handy .Net (2.0 -> 3.5 and 4.0) libraries that can be used to generate a correctly formatted CSV file.

Also, if there are any sets of rules for generating CSV files.

There are a lot of details of CSV readers and parsing CSV files, however not as many about writing (ok, I know it is just the opposite :P ).

http://www.codeproject.com/KB/database/CsvReader.aspx

Any help would be much appreciated :)

I found another article with some more detailed CSV rules: http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm

A neat 3rd party library is Linq-to-CSV (not framework library): http://www.codeproject.com/KB/linq/LINQtoCSV.aspx

Thanks for your help everyone. I have decided that the best solution will be to create a simple static class that will do the special character replacement (that Chris mentioned).

If I had a need for Linq querying my CSV files, I would look at the CodeProjects implementation of Linq-to-CSV.

Thanks again :)

like image 658
Russell Avatar asked Nov 06 '09 00:11

Russell


People also ask

How do I save a CSV file in C#?

ToString(); string csv = string. Format("{0},{1}\n", first, second); File. WriteAllText(filePath, csv); The whole function runs inside a loop, and every row should be written to the csv file.

What is CSV file in C#?

CSV (Comma Separated Values) is a very popular import and export data format used in spreadsheets and databases. Each line in a CSV file is a data record. Each record consists of one or more fields, separated by commas.


4 Answers

CsvHelper (a library I maintain) also available via NuGet.

CsvHelper can automatically write your class objects to a file for you.

var myObj = new MyCustomClass {     Prop1 = "one",     Prop2 = 2 }; var streamWriter = // Create a writer to somewhere... var csvWriter = new CsvWriter( streamWriter );  // You can write a single record. csvWriter.WriteRecord( myObj );  // You can also write a collection of records. var myRecords = new List<MyCustomClass>{ myObj }; csvWriter.WriteRecords( myRecords ); 
like image 88
Josh Close Avatar answered Sep 29 '22 10:09

Josh Close


If there are any commas in your cell, surround the entire cell with double quotes, eg:

cell 1,cell 2,"This is one cell, even with a comma",cell4,etc 

And if you want a literal double quote, do two of them, eg:

cell 1,cell 2,"This is my cell and it has ""quotes"" in it",cell 4,etc 

As for dates, stick to ISO format, and you should be fine (eg yyyy-mm-dd hh:mm:ss)

like image 26
Chris Avatar answered Sep 29 '22 11:09

Chris


I would just like to add there's an RFC that specifies the CSV format which is what I would regard as the canonical source.

like image 30
Richard Nienaber Avatar answered Sep 29 '22 11:09

Richard Nienaber


I've used filehelpers extensively and it's pretty awesome for generating CSVs.

like image 20
lomaxx Avatar answered Sep 29 '22 09:09

lomaxx