使用.NET编写一个CSV文件 [英] Writing a CSV file in .net
问题描述
我有一个要求,一个数据集导出为CSV文件。
I have a requirement to export a dataset as a CSV file.
我花了一段时间寻找一套规则去了,实现写CSV文件时,有相当多的规则和例外。
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.
<一个href="http://knab.ws/blog/index.php?/archives/3-CSV-file-parser-and-writer-in-C-Part-1.html">http://knab.ws/blog/index.php?/archives/3-CSV-file-parser-and-writer-in-C-Part-1.html <一href="http://bytes.com/topic/c-sharp/answers/236875-problems-streamwriter-output-csv">http://bytes.com/topic/c-sharp/answers/236875-problems-streamwriter-output-csv <一href="http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/0073fcbb-adab-40f0-b768-4bba803d3ccd">http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/0073fcbb-adab-40f0-b768-4bba803d3ccd
所以现在还不是用逗号分隔字符串的一个简单的过程,我已经寻找现有的CSV作家或者第三方或(希望!)包含在.NET框架。
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.
编辑:New链接: <一href="http://www.thinqlinq.com/Post.aspx/Title/LINQ-to-CSV-using-DynamicObject-and-TextFieldParser">http://www.thinqlinq.com/Post.aspx/Title/LINQ-to-CSV-using-DynamicObject-and-TextFieldParser
使用TextFieldParser是一个用VB对象(可以从C#中引用),它会自动解析CSV文件。 :)
The TextFieldParser is a VB object (can be referenced from C#) that will automatically parse CSV files. :)
我想知道是否有人知道任何方便的.NET。(2.0 - > 3.5和4.0),可用于生成一个格式正确的CSV文件的库
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.
此外,如果有任何的规则集生成的CSV文件。
Also, if there are any sets of rules for generating CSV files.
有很多的CSV读者的细节和解析CSV文件,但没有那么多的关于编写(好吧,我知道这是正好相反:P)。
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 ).
<一个href="http://www.$c$cproject.com/KB/database/CsvReader.aspx">http://www.$c$cproject.com/KB/database/CsvReader.aspx
任何帮助将是非常美联社preciated:)
Any help would be much appreciated :)
我发现了一些更详细的CSV规则的另一篇文章: <一href="http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm">http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm
I found another article with some more detailed CSV rules: http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm
有一个整洁的第三方库是LINQ到CSV(没有框架库): <一href="http://www.$c$cproject.com/KB/linq/LINQtoCSV.aspx">http://www.$c$cproject.com/KB/linq/LINQtoCSV.aspx
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).
如果我有需要的LINQ查询我的CSV文件,我想看看codeProjects执行LINQ到CSV。
If I had a need for Linq querying my CSV files, I would look at the CodeProjects implementation of Linq-to-CSV.
再次感谢:)
推荐答案
如果有单元中的任何逗号,环绕整个小区用双引号,例如:
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
至于日期,坚持ISO格式,并且你应该罚款(如YYYY-MM-DD HH:MM:SS)
As for dates, stick to ISO format, and you should be fine (eg yyyy-mm-dd hh:mm:ss)
这篇关于使用.NET编写一个CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!