anonymous
anonymous

Reputation: 6885

Exporting to CSV - C#

I need to export a generic list to CSV. Obviously I could write my own, but would like to avoid this!

I am able to google and find a lot of CSV parsers, but not many writers. I have downloaded FileHelpers but it doesn't properly escape output.

For instance if a field is equal to

,,",,,

the output is simply:

,,",,,

For the field. I would expect:

",,"",,,"

correct?

Any suggestions, or should I just do it myself?

Upvotes: 3

Views: 2034

Answers (4)

stb
stb

Reputation: 872

I can really recommend CsvHelper by Josh Close for reading and writing csv format. CsvHelper will handle most common use cases, including escaping of strings with special characters.

You can use simple methods to read and write (lists of) objects or manually read/write any field you want.

Upvotes: 0

Forgotten Semicolon
Forgotten Semicolon

Reputation: 14130

FileHelpers will do what you ask with the FieldQuoted attribute on your field in the record mapping class.

Upvotes: 4

Kevin Buchan
Kevin Buchan

Reputation: 2868

Check out RFC 4180. I had to write a XSL to convert XML to CSV and this made it very easy to know what the 'right' thing is to do.

http://tools.ietf.org/rfc/rfc4180.txt

Upvotes: 4

Bryan Rowe
Bryan Rowe

Reputation: 9323

With how simple it is to make your own, I say write you own class for this. You could handle any nuances on your own pretty easily.

Upvotes: 3

Related Questions