动态选择写入哪些 CSV 字段
我正在使用 FileHelpers 将一些数据写入 CSV 文件。 FileHelpers 很棒,因为它让我可以使用 FileHelper 转换器轻松格式化各个字段。以下是 FileHelpers DelimitedRecord 的示例:
[DelimitedRecord(",")]
public class ShippedRecord
{
public string Customer;
#quouted as can contain ',' characters
[FieldQuoted]
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;
}
我希望用户能够通过配置文件指定他们想要将哪些字段写入 CSV 文件。使用 FileHelpers,如何动态更改写入 CSV 文件的字段?我知道我可能可以使用 DataTable 之类的东西,但我不确定如何格式化字段(引用值、日期格式等),如果可能的话,我更愿意使用 FileHelpers 提供的简单转换器。如果不可能,如何使用数据表按照上面的示例记录格式化字段?
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:
[DelimitedRecord(",")]
public class ShippedRecord
{
public string Customer;
#quouted as can contain ',' characters
[FieldQuoted]
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
CsvEngine.DataTableToCsv 方法(DataTable、String、CsvOptions)
重载并设置“CsvOptions”。似乎有一些关于字段格式、引用值、日期格式等的配置设置可用(不过我自己还没有尝试过)
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)