使用 FileHelpers 动态创建 CSV 文件
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
事实上,该库现在只允许读取运行时记录,但出于写入目的,您可以使用 DataTableToCsv 方法,如下所示:
如果有帮助,请告诉我。
In fact the library only allows now to read runtime records but for writing purpouses you can use the DataTableToCsv method like this:
Let me known if that helps.
我知道这是一个老问题,但我自己也遇到了同样的问题,并花了一些时间寻找解决方案,所以我决定分享我的发现。
如果您使用 FileHelpers RunTime Records 创建定义,您可以使用反射填充相同的定义。
例如,如果您创建一个定义,
现在您可以使用 FileHelpers 创建的相同类型来填充您的值,如下所示:
然后将其写入文件或字符串:
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
Now you can use same type created by FileHelpers to populate your values as follows:
And then write it to file or string: