Filehelpers - 在每个字段周围用双引号导出,除非字段为 blank/null
Filehelpers - Export with double quotes around each field EXCEPT when field is blank/null
我可能在搜索时错过了答案,但我有一个文件需要所有字段都用双引号引起来,除非字段为空白、缺失或为空,否则只会输入逗号。
我正在使用 [FieldQuoted('"', QuoteMode.AlwaysQuoted)]
示例输出如下:
"Mary","Smith","555-555-5555","","1234","","3141 Pi Cr."
但我需要输出看起来像这样:
"Mary","Smith","555-555-5555",,"1234",,"3141 Pi Cr."
有使用 Filehelpers 的建议吗?
您可以使用 INotifyWrite
事件在写入文件之前修改输出。
例如
[DelimitedRecord(",")]
class Product : INotifyWrite // <-- implement events
{
[FieldQuoted(QuoteMode.AlwaysQuoted)]
public string Name;
[FieldQuoted(QuoteMode.AlwaysQuoted)]
public string Description;
[FieldQuoted(QuoteMode.AlwaysQuoted)]
public string Size;
public void BeforeWrite(BeforeWriteEventArgs e)
{
}
public void AfterWrite(AfterWriteEventArgs e)
{
// replace any occurrences of ,"", with ,,
e.RecordLine = e.RecordLine.Replace(",\"\",", ",,");
}
}
用这个解决问题:
[FieldQuoted('"', QuoteMode.OptionalForWrite)]
我可能在搜索时错过了答案,但我有一个文件需要所有字段都用双引号引起来,除非字段为空白、缺失或为空,否则只会输入逗号。
我正在使用 [FieldQuoted('"', QuoteMode.AlwaysQuoted)]
示例输出如下:
"Mary","Smith","555-555-5555","","1234","","3141 Pi Cr."
但我需要输出看起来像这样:
"Mary","Smith","555-555-5555",,"1234",,"3141 Pi Cr."
有使用 Filehelpers 的建议吗?
您可以使用 INotifyWrite
事件在写入文件之前修改输出。
例如
[DelimitedRecord(",")]
class Product : INotifyWrite // <-- implement events
{
[FieldQuoted(QuoteMode.AlwaysQuoted)]
public string Name;
[FieldQuoted(QuoteMode.AlwaysQuoted)]
public string Description;
[FieldQuoted(QuoteMode.AlwaysQuoted)]
public string Size;
public void BeforeWrite(BeforeWriteEventArgs e)
{
}
public void AfterWrite(AfterWriteEventArgs e)
{
// replace any occurrences of ,"", with ,,
e.RecordLine = e.RecordLine.Replace(",\"\",", ",,");
}
}
用这个解决问题:
[FieldQuoted('"', QuoteMode.OptionalForWrite)]