c# – Dyanamically selecting which CSV fields are written – Education Career Blog

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:

public class ShippedRecord   

    public string Customer;

    #quouted as can contain ',' characters
    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?


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)

Leave a Comment