使用CsvHelper 6.1.0编写CSV文件时,文档只提到一次在写入时控制标题名称:
“写作时,只使用名字。”
我的代码符合给定的示例:
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");
写操作期间不写入空格!
ReportingGroup,商家,ActivityDate,SettlementDate
我的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.");
}
答案 0 :(得分:2)
您需要在写作时注册您的班级地图。看起来你已经阅读了它,但它在写作时却缺失了。