使用CsvHelper 6.1.0编写CSV文件时,文档仅一次提到控制写入标题名称的方法:
"When writing, only the first name is used."
我的代码与给出的示例相符:
"When writing, only the first name is used."
我的代码与给出的示例相符:
Map(m => m.ReportingGroup).Name("Reporting Group");
Map(m => m.Merchant);
Map(m => m.ActivityDate).Name("Activity Date");
Map(m => m.SettlementDate).Name("Settlement Date");
在写入操作期间不会写入空格!
报告组、商家、活动日期、结算日期
如果需要,这是我的Read()和Write():
public static IEnumerable<TR> Read<TR, TMap>(string filename)
where TMap : ClassMap
{
List<TR> records;
using (var sr = new StreamReader(filename))
{
var csv = new CsvReader(sr);
csv.Configuration.RegisterClassMap<TMap>();
records = csv.GetRecords<TR>().ToList();
}
return records;
}
public static void Write<T>(IEnumerable<T> records, string filename, Logger logger)
{
var enumerable = records as IList<T> ?? records.ToList();
logger?.Debug($"Writing {enumerable.Count} records to file: {filename}");
using (var fcreate = File.Open(filename, FileMode.Create))
using (var writer = new StreamWriter(fcreate))
{
var csv = new CsvWriter(writer);
csv.WriteRecords(enumerable);
}
logger?.Debug("Writing complete.");
}