AJM
AJM

Reputation: 32490

.Net producing CSV file

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 utility. Do any such libraries/utilities exists?

Upvotes: 2

Views: 962

Answers (4)

Josh Close
Josh Close

Reputation: 23383

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

Upvotes: 1

Tom
Tom

Reputation: 26829

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

Upvotes: 1

o.k.w
o.k.w

Reputation: 25810

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#

Upvotes: 1

Konamiman
Konamiman

Reputation: 50273

Take a look at this: http://www.codeproject.com/KB/aspnet/ExportClassLibrary.aspx

Upvotes: 2

Related Questions