导出到 CSV - C#
我需要将通用列表导出到 CSV。显然我可以自己写,但想避免这种情况!
我可以通过谷歌搜索并找到很多 CSV 解析器,但作者不多。我已经下载了 FileHelpers,但它无法正确转义输出。
例如,如果某个字段等于,则
,,",,,
输出很简单:
,,",,,
对于该字段。我期望:
",,"",,,"
正确吗?
有什么建议吗,还是我应该自己做?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查看 RFC 4180。我必须编写一个 XSL 将 XML 转换为 CSV,这使得很容易知道应该做什么“正确”的事情。
http://tools.ietf.org/rfc/rfc4180.txt
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 属性执行您所要求的操作。
FileHelpers will do what you ask with the FieldQuoted attribute on your field in the record mapping class.
由于创建自己的课程非常简单,我说为此编写您自己的类。您可以自己轻松处理任何细微差别。
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.
我真的可以推荐 Josh Close 的 CsvHelper 来读取和写入 csv 格式。 CsvHelper 将处理最常见的用例,包括转义带有特殊字符的字符串。
您可以使用简单的方法来读取和写入对象(列表)或手动读取/写入您想要的任何字段。
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.