LinqToCSV 似乎在空字符串周围添加引号
有没有设置可以关闭这个?
在文档中找不到任何内容。
还尝试发送空值而不是空字符串..相同的结果
这是使用 LinqToCSV 生成的示例 CSV 输出。
第 5,6,7 列是空字符串..它们在最终 CSV 输出中被证明是带引号的字符串
3223,2876,5591,9171068,"","","",10000442,A1,"",012000016431,2.50,8,0,8,20150108224612
3223,2876,5591,9171068,"","","",10000442,A2,"",012000016431,2.50,8,0,8,20150108224612
3223,2876,5591,9171068,"","","",10000442,A3,"",012000043000,1.75,8,0,8,20150108224612
3223,2876,5591,9171068,"","","",10000442,A4,"",012000043000,1.75,8,0,8,20150108224612
3223,2876,5591,9171068,"","","",10000442,A5,"",012000043000,1.75,8,0,8,20150108224612
3223,2876,5591,9171068,"","","",10000442,A6,"",012000110467,1.75,8,0,8,20150108224612
这是供引用的示例代码
CsvFileDescription fd = new CsvFileDescription();
fd.FirstLineHasColumnNames = false;
fd.QuoteAllFields = false;
fd.EnforceCsvColumnAttribute = true;
fd.SeparatorChar = Globals.Separator[0];
CsvContext cc = new CsvContext();
cc.Write(data, txtFileName, fd);
数据的所有属性都是纯字符串
例如其中一列被定义为
[CsvColumn(FieldIndex = 16, CanBeNull = true)]
public string TimeStamp { get; set; }
最佳答案
从 LinqToCsv v1.5 开始,此行为不可重现。当我将 TimeStamp
字段的值设置为 null
时,该值没有被引用。然而,引用了 String.Empty
的值。
考虑以下:
void Main()
{
var fd = new CsvFileDescription();
fd.FirstLineHasColumnNames = false;
fd.QuoteAllFields = false;
fd.EnforceCsvColumnAttribute = true;
fd.SeparatorChar = ',';
var txtFileName = @"C:\temp\linqtocsv.csv";
var data = new[] { new Data { TimeStamp = null, Foo = null, Bar = "" } };
var cc = new CsvContext();
cc.Write(data, txtFileName, fd);
}
class Data
{
[CsvColumn(FieldIndex = 0, CanBeNull = true)]
public string TimeStamp { get; set; }
[CsvColumn(FieldIndex = 1, CanBeNull = true)]
public string Foo { get; set; }
[CsvColumn(FieldIndex = 2, CanBeNull = true)]
public string Bar { get; set; }
}
输出的文件内容为:
,,""
您可以使用 this LinqPad query 自己尝试一下。