List Exporter To CSV

Today I wrote a small utility class that allows any List<T> to be exported to a CSV string or a CSV file using a nice Fluent interface.

This is what it allows:

  1. Take any List<T>, where T could be any class
  2. Let the using code specify what columns should be exported
  3. Use a Fluent API (as they are all the rage these days
  4. Allow auto header named to be obtained using using Expression trees
  5. Allow the the user to supply custom header names
  6. Allow the user to also supply custom format strings for the data being exported
  7. Allow the data to be exported to a CSV file OR allow the data to be exported to a CSV string

 

And here is an example of how you might use it

//Get it as a String result, using automatic Headers, but formatted Columns, 
//and standard "," seperator
String resultsWithDefaultHeadersAndFormattedColumns = people.GetExporter()
    .AddExportableColumn((x) => x.Age, 
                  customFormatString:"The Person Age Is {0}")
    .AddExportableColumn((x) => x.Name, 
                  customFormatString: "The Persons Name {0}")
    .AsCSVString();

If you want to know more you can read the full article over at :

http://www.codeproject.com/KB/miscctrl/CSVListExporter.aspx

About these ads

4 thoughts on “List Exporter To CSV

  1. Isn’t it easier to Linq select an anonymous type then feed it to a .ToCsv extension? The anonymous type would have all the columns you need, reflection would give you the properties only on the first record so it would be fast and an optional parameter could rename the columns to support empty spaces and such.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s