1

FileHelpers supports a feature called "RunTime Records" which lets you read a Fixed length text file into a DataTable when you don't know the layout until runtime.

Is it possible to use FileHelpers to export a Fixed length file at runtime in the same manner?

shamp00
  • 11,106
  • 4
  • 38
  • 81
Manoj
  • 60
  • 1
  • 7

1 Answers1

3

Here's a working sample for export using a DataTable as source.

public static class DataTableExtensions
{
    public static List<object> DataTableToList(this DataTable table, Type type)
    {
        List<object> list = new List<object>();
        foreach (var row in table.AsEnumerable())
        {
            object obj = Activator.CreateInstance(type);
            foreach (var field in obj.GetType().GetFields())
            {
                FieldInfo fieldInfo = obj.GetType().GetField(field.Name);
                fieldInfo.SetValue(obj, Convert.ChangeType(row[field.Name], fieldInfo.FieldType));
            }
            list.Add(obj);
        }

        return list;
    }
}

class Program
{
    private static void Main(string[] args)
    {
        var cb = new FixedLengthClassBuilder("Customer");

        cb.AddField("BirthDate", 8, typeof(DateTime));
        cb.LastField.Converter.Kind = ConverterKind.Date;
        cb.LastField.Converter.Arg1 = "ddMMyyyy";
        cb.LastField.FieldNullValue = DateTime.Now;

        cb.AddField("Name", 3, typeof(string));

        cb.AddField("Age", 3, typeof(int));
        cb.LastField.TrimMode = TrimMode.Both;

        Type recordClass = cb.CreateRecordClass();

        var dataTable = new DataTable("Customer");
        dataTable.Columns.Add("BirthDate", typeof(DateTime));
        dataTable.Columns.Add("Name", typeof(string));
        dataTable.Columns.Add("Age", typeof(int));

        dataTable.Rows.Add(new DateTime(1972, 8, 14), "Joe", 42);
        dataTable.Rows.Add(new DateTime(1971, 9, 15), "Tine", 43);

        var list = dataTable.DataTableToList(recordClass);

        var engine = new FileHelperEngine(recordClass);
        engine.WriteFile(filename, list);

        Console.ReadKey();
    }
}
shamp00
  • 11,106
  • 4
  • 38
  • 81
  • Thanks Shamp00, The code you provided is for import and its working fine. Actually i am looking for the solution to export the DataTable data to Fixed length file using "Run time class/records". Appreciate your help. – Manoj Jul 07 '15 at 13:07
  • Gotcha. It's easy if you use `dynamic` variables. I have edited my answer. – shamp00 Jul 07 '15 at 17:34
  • Thanks Shamp00, This is very close i am looking. But in my program i actually gets the data from DB into DataTable to do export. Do you have any idea how to convert DataTable into IEnumerable collection of d"Runtime class/Record" type. Here record class fields information is user configurable (maintaining the fields information at DB) and dynamic . Could you please help on this – Manoj Jul 08 '15 at 13:47
  • Then it's probably best to do the `DataTable` field mapping with a helper class. I have edited my answer again. – shamp00 Jul 09 '15 at 09:13
  • Perfect solution Shamp00!!!! Great example. Again Thank you so much for your help Shamp00!!! – Manoj Jul 09 '15 at 13:57