简体   繁体   中英

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?

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

FileHelpers将使用记录映射类中字段的FieldQuoted属性执行您所要求的操作

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM