在.net中写入CSV文件 [英] Writing a CSV file in .net

查看:232
本文介绍了在.net中写入CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将数据集导出为CSV文件。



我花了一段时间搜索一组规则,并意识到在写CSV文件时有很多规则和例外。



http://knab.ws/blog/index.php?/archives/3-CSV-file-parser-and-writer-in-C-Part-1.html < a>
http://bytes.com / topic / c-sharp / answers / 236875-problems-streamwriter-output-csv
http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/0073fcbb-adab-40f0-b768-4bba803d3ccd



现在,这不是一个用逗号分隔字符串的简单过程,我已经搜索了一个现有的CSV作者第三方或(希望!)包括在.net框架中。



编辑:新连结:
http://www.thinqlinq.com/Post.aspx/Title/LINQ-to-CSV-using-DynamicObject-and-TextFieldParser

TextFieldParser是一个VB对象(可以从C#引用),它将自动解析CSV文件。 :)



我想知道是否有人知道任何方便的.Net(2.0 - > 3.5和4.0)库,可以用来生成格式正确的CSV文件。 p>

此外,如果有用于生成CSV文件的任何规则集。



有很多CSV阅读器和解析CSV文件,但是没有多少关于写作(确定,我知道恰恰相反:P)。



http://www.codeproject.com/KB/database/CsvReader.aspx



任何帮助将非常感谢:)



我发现另一篇文章有​​一些更详细的CSV规则:
http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm

一个整齐的第三方库是Linq-to-CSV(不是框架库):
http://www.codeproject.com/KB/linq/LINQtoCSV.aspx



谢谢为您的帮助大家。
我决定最好的解决方案是创建一个简单的静态类,将做特殊的字符替换(Chris提到)。



如果我有需要Linq查询我的CSV文件,我会看看Linq-to-CSV的CodeProjects实现。



再次感谢:)

解决方案

如果您的单元格中有逗号,则用双引号括起整个单元格,例如:

  cell 1,cell 2,这是一个单元格,即使有逗号,cell4等等

如果你想要一个字面双引号,请做其中两个,例如:

 单元格1,单元格2,这是我的单元格,它有报价在其中,单元格4等

对于日期,坚持ISO格式,你应该很好(例如yyyy-mm-dd hh:mm:ss)


I have a requirement to export a dataset as a CSV file.

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.

http://knab.ws/blog/index.php?/archives/3-CSV-file-parser-and-writer-in-C-Part-1.html http://bytes.com/topic/c-sharp/answers/236875-problems-streamwriter-output-csv http://social.msdn.microsoft.com/forums/en-US/csharpgeneral/thread/0073fcbb-adab-40f0-b768-4bba803d3ccd

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.

Edit: New link: http://www.thinqlinq.com/Post.aspx/Title/LINQ-to-CSV-using-DynamicObject-and-TextFieldParser

The TextFieldParser is a VB object (can be referenced from C#) that will automatically parse CSV files. :)

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.

Also, if there are any sets of rules for generating CSV files.

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 ).

http://www.codeproject.com/KB/database/CsvReader.aspx

Any help would be much appreciated :)

I found another article with some more detailed CSV rules: http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm

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).

If I had a need for Linq querying my CSV files, I would look at the CodeProjects implementation of Linq-to-CSV.

Thanks again :)

解决方案

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

As for dates, stick to ISO format, and you should be fine (eg yyyy-mm-dd hh:mm:ss)

这篇关于在.net中写入CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆