c# – 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文件.我不知道我做错了什么

完全例外是:

An exception of type ‘CsvHelper.CsvMissingFieldException’ occurred in CsvHelper.dll but was not handled in user code

Additional information: Fields ‘PartNumber’ do not exist in the CSV file.

这是一个示例头文件和第一行:

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,N,EA,879345348000086,MAC,US,13.80,

解决方法

您的字段名称文件标题不符合空格.在PulProduct中,第一个字段是“PartNumber”.在您的示例文件中,第一列我们“零件号”.在CsvConfiguration对象中将IgnoreHeaderWhiteSpace设置为true将会处理此问题.
using (TextReader prodFile = System.IO.File.OpenText(filePath))
{
    CsvReader csv = new CsvReader(prodFile);
    csv.Configuration.IgnoreHeaderWhiteSpace = true;
    List<PulProduct> prodList = csv.GetRecords<PulProduct>().ToList();
}

相关文章

在项目中使用SharpZipLib压缩文件夹的时候,遇到如果目录较深,则压缩包中的文件夹同样比较深的问题。比...
项目需要,几十万张照片需要计算出每个照片的特征值(调用C++编写的DLL)。 业务流程:选择照片...
var array = new byte[4]; var i = Encoding.UTF8.GetBytes(100.ToString(&quot;x2&quot;));//...
其实很简单,因为Combox的Item是一个K/V的object,那么就可以把它的items转换成IEnumerable&lt;Dic...
把.net4.6安装包打包进安装程序。 关键脚本如下: 头部引用字符串对比库 !include &quot;WordFunc....
项目需求(Winform)可以批量打印某个模板,经过百度和摸索,使用iTextSharp+ZXing.NetʿreeSp...