编程语言
首页 > 编程语言> > C#CsvHelper.ValidationException – 为什么?

C#CsvHelper.ValidationException – 为什么?

作者:互联网

我想在C#console app中使用CSVHelper.我有一个例外:

CsvHelper.ValidationException: ‘Header matching [‘Numer Dokumentu’]
names at index 0 was not found.

我不知道为什么因为这个标题在他的csv文件中.

这是我的Program.cs

var packs = new List<Pack>();
            using (var streamReader = File.OpenText("C:/.../file.csv"))
            {                
                var reader = new CsvReader(streamReader);
                reader.Configuration.RegisterClassMap<PackMap>();
                packs = reader.GetRecords<Pack>().ToList();
            }

和Pack.cs

public class Pack {   
    public string NrDoc { get; set; }
    public string recipientName { get; set; }
    public string recipientAdress { get; set; }
    public string recipientCity { get; set; }
    public string packValue { get; set; }
    public string packInfo { get; set; }
     }

和PackMap.cs

 sealed class PackMap : ClassMap<Pack>
{
    public PackMap()
    {

        AutoMap();
        Map(m => m.NrDoc).Name("Numer Dokumentu");
        Map(m => m.recipientName).Name("Kontrahent");
        Map(m => m.recipientAdress).Name("Ulica");
        Map(m => m.recipientCity).Name("Miasto");
        Map(m => m.packValue).Name("Brutto");
        Map(m => m.packInfo).Name("Opis");
    }
}

在“PackMap.cs”中,我尝试使用Index(0),Index(1)等,没有任何变化.
谁会告诉我我做错了什么?
我必须在CSV文件中使用不同的标题名称,在C#中使用不同的变量

这是我的csv文件:

Numer Dokumentu;Status;Data wyst.;Magazyn;Kontrahent;Ulica;Miasto;Netto;Brutto;Opis
FA/3/08/2017/1;;16.08.2017;MAGAZYN;Damianowa Firma;Nowa Lucyna Herc;Lublin;87;20;107;25;Wystawić fakturę. Uwagi klienta:   1
FA/1/10/2017/6;;28.10.2017;MAGAZYN;IBIS Marek Jeż;Jana Pawła II;Szubin;241;00;296;43;Wysyłka
FA/2/10/2017/6;;28.10.2017;MAGAZYN;Netia  S.A.;ul. Poleczki 13;Warszawa;782;28;962;20;Wysyłka pobranie

解决方法:

您的文件不以逗号分隔,因此您需要更改要使用的配置;而不是逗号.

reader.Configuration.Delimiter = ";";

标签:c,csvhelper
来源: https://codeday.me/bug/20190627/1305813.html