Подтвердить что ты не робот

CSVReader - Поля не существуют в файле CSV

Я использую пакет CSVHelper NuGet и получаю сообщение об ошибке "Поля не существуют в CSV файле". Вот мой код:

using (TextReader prodFile = System.IO.File.OpenText(filePath))
{
    CsvReader csv = new CsvReader(prodFile);
    List<PulProduct> prodList = csv.GetRecords<PulProduct>().ToList();
}

класс

public class PulProduct
    {
        public string PartNumber { get; set; }
        public string PPartNumber { get; set; }
        public string VPartNumber { get; set; }
        public string VPPartNumber { get; set; }
        public string Status { get; set; }
        public string Description { get; set; }
        public decimal ORetail { get; set; }
        public decimal CSRetail { get; set; }
        public decimal BDPrice { get; set; }
        public decimal YDPrice { get; set; }
        public string Hazardous { get; set; }
        public string TruckPart { get; set; }
        public string PartAddDate { get; set; }
        public int AvailabilityWI { get; set; }
        public int AvailabilityNY { get; set; }
        public int AvailabilityTX { get; set; }
        public int AvailabilityCA { get; set; }
        public int AvailabilityNV { get; set; }
        public int AvailabilityNC { get; set; }
        public int AvailabilityNational { get; set; }
        public string Trademark { get; set; }
        public string AdPolicy { get; set; }
        public string PriceChanged { get; set; }
        public string UOM { get; set; }
        public string UPC { get; set; }
        public string BrandName { get; set; }
        public string Country { get; set; }
        public string Weight { get; set; }
        public string Closeout { get; set;}
        public string NoShipToCA { get; set; }
        public string Notes {get; set; }
    }

В документации CSVHelper указано, что CSVHelper автоматически сопоставляет мой класс с CSV файлом. Я не уверен, что я делаю неправильно.

Полное исключение:

Исключение типа 'CsvHelper.CsvMissingFieldException' произошло в CsvHelper.dll, но не было обработано в коде пользователя

Дополнительная информация: Поля "PartNumber" не существуют в CSV файле.

Вот пример заголовка и первой строки:

Part Number,Punctuated Part Number,Vendor Part Number,Vendor Punctuated Part Number,Part Status,Part Description,Original Retail,Current Suggested Retail,Base Dealer Price,Your Dealer Price,Hazardous Code,Truck Part Only,Part Add Date,WI Availability,NY Availability,TX Availability,CA Availability,NV Availability,NC Availability,National Availability,Trademark,Ad Policy,Price Changed Today,Unit of Measure,UPC Code,Brand Name,Country of Origin,Weight,Closeout Catalog Indicator,NoShipToCA, Notes
0023451,001-0901,0067401,067-0401,S,4-1 SYS OBR CB350/4,399.95,352.95,384.40,214.40,,,19341102,0,0,0,0,0,0,0,,,N,EA,879345348000086,MAC,US,13.80,N, ,
4b9b3361

Ответ 1

Имена полей и заголовки столбцов файла не совпадают из-за пробелов. В PulProduct первое поле - "PartNumber". В вашем примере файла, в первом столбце "Номер детали". Установка IgnoreHeaderWhiteSpace в true в объекте CsvConfiguration будет обрабатывать это.

using (TextReader prodFile = System.IO.File.OpenText(filePath))
{
    CsvReader csv = new CsvReader(prodFile);
    csv.Configuration.IgnoreHeaderWhiteSpace = true;
    List<PulProduct> prodList = csv.GetRecords<PulProduct>().ToList();
}

Ответ 2

Несмотря на то, что в документации указано, что CSVHelper автоматически сопоставляет класс с CSV файлом, вам все равно нужно вручную написать карту, которая будет отображаться в файл CSV.