views:

56

answers:

1

I am using FileHelpers to write some data out to a CSV file. FileHelpers is great because it lets me easily format the various fields using the FileHelper converters. Here is an example of a FileHelpers DelimitedRecord:

[DelimitedRecord(",")]   
public class ShippedRecord   
{

    public string Customer;

    #quouted as can contain ',' characters
    [FieldQuoted]
    public string Address;

    [FieldConverter(ConverterKind.Date, "yyyy-MM-dd")]   
    public DateTime ShippedDate;

    [FieldConverter(ConverterKind.Date, "yyyy-MM-dd")]
    public DateTime ReceivedDate;

    public string DaysTillDelivery;

    public string DeliveryStatus;   
}

I would like the user be able to specify, via a config file, which of the fields they want written to the CSV file. Using FileHelpers, how do I dynamically change which fields are written out to the CSV file? I know I could probably use something like a DataTable, but I'm not sure how to format the fields (quoted values, date format etc) and I'd prefer to use the simple converters provided by FileHelpers if possible. If not possible, how do I go about formatting the fields as per the example record above using a DataTable?

+1  A: 

You possibly could use the CsvEngine.DataTableToCsv Method (DataTable, String, CsvOptions) overload and set the 'CsvOptions'. There seems to some configuration settings available with respect to field formats, quoted values, date formats etc

(I have not tried this myself though)

Ahmad
I looked at this, but unfortunately the doc says that it only allows the delimiter to be set.
swisstony
@swisstony - you can set other public properties of `CsvOptions` eg `CsvOptions.DateFormat`. Yes, constructor is limited to the delimiter.
Ahmad
@Ahmad Thank you, that looks good. I can see how I can set the date format now. However, how do I specify that specific columns should have their values quoted?
swisstony
@swisstony - you got me there - I think that the CsvOptions work in a general way and are applied to all fields.
Ahmad