使用 FileHelpers 动态创建 CSV 文件

发布于 2024-08-18 08:14:34 字数 481 浏览 11 评论 0原文

FileHelpers 支持一项名为“运行时记录”的功能,当您直到运行时才知道布局时,该功能可让您将 CSV 文件读入数据表中。

是否可以使用 FileHelpers 以相同的方式在运行时创建 CSV 文件?

根据某些用户输入,必须创建的 CSV 文件将具有只能在运行时知道的不同字段。我可以按照阅读部分中的描述为 FileHelper 引擎创建所需的类型,但我无法弄清楚我的数据需要采用什么格式才能写入。

var engine = new FileHelpers.FileHelperEngine(GenerateCsvType());

engine.WriteStream(context.Response.Output, dontKnow);

编辑

另外,任何人都可以建议一个好的 CSV 库,它可以在运行时之前不知道其字段的情况下创建 CSV 文件吗?例如,从数据表创建 CSV 文件。

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

Is it possible to use FileHelpers to create a CSV file at runtime in the same manner?

Based on some user input, the CSV file that must be created will have different fields that can only be known at runtime. I can create the needed Type for the FileHelper engine as described in their reading section, but I can't figure out what format my data needs to be in to be written.

var engine = new FileHelpers.FileHelperEngine(GenerateCsvType());

engine.WriteStream(context.Response.Output, dontKnow);

EDIT

Alternatively, can anyone suggest a good CSV library that can create a CSV file without knowing its fields until runtime? For example, create a CSV file from a DataTable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你没皮卡萌 2024-08-25 08:14:34

事实上,该库现在只允许读取运行时记录,但出于写入目的,您可以使用 DataTableToCsv 方法,如下所示:

CsvEngine.DataTableToCsv(dt, filename);

如果有帮助,请告诉我。

In fact the library only allows now to read runtime records but for writing purpouses you can use the DataTableToCsv method like this:

CsvEngine.DataTableToCsv(dt, filename);

Let me known if that helps.

若言繁花未落 2024-08-25 08:14:34

我知道这是一个老问题,但我自己也遇到了同样的问题,并花了一些时间寻找解决方案,所以我决定分享我的发现。

如果您使用 FileHelpers RunTime Records 创建定义,您可以使用反射填充相同的定义。

例如,如果您创建一个定义,

        DelimitedClassBuilder cb = new DelimitedClassBuilder("Customers", ",");
        cb.AddField("StringField", "string");

        Type t = cb.CreateRecordClass();
        FileHelperEngine engine = new FileHelperEngine(t); 

现在您可以使用 FileHelpers 创建的相同类型来填充您的值,如下所示:

        object customClass = Activator.CreateInstance(t);
        System.Reflection.FieldInfo field = customClass.GetType().GetField("StringField", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
        if (field != null)
        {
            field.SetValue(customClass, "StringValue");
        }

然后将其写入文件或字符串:

        string line = engine.WriteString(new object[] { customClass });

I know this is an old question, but I ran into same issue myself and spent some time looking for solution, so I decided to share my findings.

If you are using FileHelpers RunTime Records to create your definition you can populate same definition using reflection.

For example if you create a definition

        DelimitedClassBuilder cb = new DelimitedClassBuilder("Customers", ",");
        cb.AddField("StringField", "string");

        Type t = cb.CreateRecordClass();
        FileHelperEngine engine = new FileHelperEngine(t); 

Now you can use same type created by FileHelpers to populate your values as follows:

        object customClass = Activator.CreateInstance(t);
        System.Reflection.FieldInfo field = customClass.GetType().GetField("StringField", System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
        if (field != null)
        {
            field.SetValue(customClass, "StringValue");
        }

And then write it to file or string:

        string line = engine.WriteString(new object[] { customClass });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文