快速帮助在这里,请使用csvhelper ...
csv:
名称,姓氏
人员地图:
public override void CreateMap()
{
Map(x => x.Name).Name("Name");
Map(x => x.LasName).Name("LastName");
}
人员类别:
public string Name { get; set; }
public string LastName { get; set; }
主要:
public void writePerson()
{
IEnumerable<Person> records;
using (var r = new CsvReader(new StreamReader("person.csv")))
{
r.Configuration.RegisterClassMap<PersonMap>();
records = r.GetRecords<Person>().ToList();
}
using (var w = new CsvWriter(new StreamWriter("person.csv")))
{
w.Configuration.RegisterClassMap<PersonMap>();
w.WriteRecord(records); //rewrite csv list
w.WriteField("John"));
w.WriteField("Doe");
w.NextRecord();
}
}
错误行:records = reader.GetRecords()。ToList();
错误:
找不到标题记录。
最佳答案
好的,所以我用以下方法修复了它:
写:
string persondata = "John, Doe";
using (FileStream fs = new FileStream("person.csv", FileMode.Append, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(fs))
{ sw.WriteLine(persondata); sw.Dispose(); }
读:
IEnumerable<Person> records;
using (var reader = new CsvReader(new StreamReader(@"person.csv")))
{
reader.Configuration.RegisterClassMap<PersonMap>();
records = reader.GetRecords<Person>();
}