views:

365

answers:

3

Hello,

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?

+3  A: 

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.

Bryan Rowe
Yah... reading about the nuances right now. If it is just commas, quotes, and newlines it might be simple enough. I was hoping that FileHelpers could be used for both reading and writing -- it would gaurantee that any file written could be read correctly -- but that doesn't seem to be the case.
eyston
+3  A: 

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

Kevin Buchan
Thanks for the link. That will be pretty good backing :)
eyston
That's all well and good, assuming that the applications generating and consuming the CSV both conform to RFC 4180 in precisely the same way. In the Real World, CSV is very much like HTML, in that very nearly nobody does it according to the standard, and that Microsoft makes a product that doesn't produce or consume the standard. With so many of the world's CSV's being created by OLD versions of Excel, the situation is very similar to when IE6 and its broken HTML/CSS interpretation controlled 98.5% of the market.
Matt Campbell
Very true Matt, but I would contend that if an app either fully conforms to the RFC or it doesn't and that if you're going to build your own tool, as the OP does, you should strive for perfection and follow the RFC.
Kevin Buchan
Postel's Law: Be conservative in what you do, be liberal in what you accept from others. http://en.wikipedia.org/wiki/Jon_Postel#Postel.27s_Law
Forgotten Semicolon
+3  A: 

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

Forgotten Semicolon
Unless I'm misunderstanding the RFC ('If fields are not enclosed with double quotes, then double quotes may not appear inside the fields.'), it is wrong in one case: [FieldQuoted('"', QuoteMode.OptionalForBoth)], with a field of 'abc"123' will output 'abc"123'. But it will catch commas so a field of 'abc,123' will output '"abc,123"'. That said, I can just make the QuoteMode not optional and quit being pedantic? :)
eyston
FileHelpers may cover cases outside of the RFC, like that one, since there will be cases of CSV files created by developers who have never read the RFC. You hadn't. ;-)
Forgotten Semicolon
I hate having three good answers!
eyston
When working on FileHelpers, we have *not* tried to be conformant to any RFC, nor have we ever claimed to be. What we *have* tried rather extensively to do is ensure compatibility with multiple versions of the extremely quirky Excel/Office CSV format, since that CSV dialect composes such a large percentage of all "Real World" business applications we've had to support for ourselves.
Matt Campbell
Thanks Matt, for the work you and Marcos do on FileHelpers.
Forgotten Semicolon