gpt4 book ai didi

c# - 使用 csvhelper 进行映射/写入协助

转载 作者:行者123 更新时间:2023-11-30 20:50:13 24 4
gpt4 key购买 nike

请在 csvhelper 上快速帮助...

数据格式:名字,姓氏

人物 map :

    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();
}
}

错误行:记录 = 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>();
}

关于c# - 使用 csvhelper 进行映射/写入协助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22737972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com