I need to create a CSV file in C#
I could do this myself and I have a java utility I wrote to do this as a reference. However it would be easier to use an existing .net library or uti开发者_如何学编程lity. Do any such libraries/utilities exists?
Take a look at this: http://www.codeproject.com/KB/aspnet/ExportClassLibrary.aspx
Even though the question is pretty, I think it is worth adding information about CsvHelper library.
CsvHelper is a library for reading and writing CSV files. Extremely fast, flexible, and easy to use
- Source code available on github repository under Microsoft Public License.
- Available as a nuget package CsvHelper
- Ability to map custom class properties to CSV fields (by field names, indexes, with custom type converters) - personally think this is one of the best features.
- Custom type converters (works with CSV read and write operation)
- Supports LINQ
- Supports different versions of .NET i.e.
2.0
,3.5
,4.5
Usually the problems lie with the reading/parsing of the CSV; writing/creating is more straightforward.
I shall not re-invent the wheel, here's a sample: A simple CSV generator from Dataset in C#
There are a few libraries that will help with writing. CsvHelper (a library I maintain) can take your objects and write them to a CSV file automatically for you.
var myCustomObjects = new List<MyCustomObject>{ ... };
var csv = new CsvHelper( File.OpenWrite( "file.csv" ) );
csv.Writer.WriteRecords( myCustomObjects );
精彩评论