RSS Feed for This PostCurrent Article

FileHelpers – A .NET Equivalent to Java Super Csv

I came across Super Csv at DZone, which has just released a new version. In fact I used Super Csv in one of my previous projects and found that is very convenient. I also need to parse Csv file in some of my .NET projects. For .NET, I use FileHelpers.

FileHelpers is a delimited (CSV) or fixed data import/export framework. Conceptually it is similiar to Super Csv. It can also be used to import/export data from different data storages (Excel, Acces, SqlServer). You can use it to strong type your flat file (fixed or delimited) simply describing a class that maps to each record of the file and work with your file like a strong typed .NET array.

The library has had a Record Class Wizard to generate the record class. The Record Class Wizard allows you to generate your record mapping class, save the definition, generate code based on snippets (Read File, Read with Generics, ReadAsync and so on).

As an example (taken from the website)

For the delimited file below.

10248|VINET|04071996|32.38
10249|TOMSP|05071996|11.61
10250|HANAR|08071996|65.83
10251|VICTE|08071996|41.34
...............

We need to create the class

[DelimitedRecord("|")]
public class Orders
{
   public int OrderID;

   public string CustomerID;

   [FieldConverter(ConverterKind.Date, "ddMMyyyy")]
   public DateTime OrderDate;

   public decimal Freight;
}

Instantiate a FileHelperEngine and Read/Write files:

FileHelperEngine engine = new FileHelperEngine(typeof(Orders));

/// to Read use:
Orders[] res = (Orders[]) engine.ReadFile("input.txt");

/// to Write use:
engine.WriteFile("output.txt", res);

Use the res array to access each item in the file, for example:

 foreach (Orders order in res)
 {
    Console.WriteLine("Order Info:");
    Console.WriteLine(order.CustomerID + " - " +
                      order.OrderDate.ToString("dd/MM/yy"));
 }


Trackback URL


RSS Feed for This Post1 Comment(s)

  1. Marcos | Oct 17, 2007 | Reply

    Hi there

    Thanks a lot for your words about the library =)

    We put a lot of effort to create it and is very motivating to see reviews like this one.

    Cheers

Sorry, comments for this entry are closed at this time.