1

FileHelpers 支持一种称为“运行时记录”的功能,当您在运行时才知道布局时,它可以让您将固定长度的文本文件读入 DataTable。

是否可以使用 FileHelpers在运行时以相同的方式导出固定长度文件?

4

1 回答 1

3

这是使用DataTable作为源导出的工作示例。

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();
    }
}
于 2015-07-07T08:25:44.193 回答