【问题标题】:CSVReader - Fields do not exist in the CSV fileCSVReader - CSV 文件中不存在字段
【发布时间】:2014-08-01 22:53:16
【问题描述】:

我正在使用 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 中,但未在用户代码中处理

附加信息:CSV 文件中不存在“PartNumber”字段。

这是一个示例标题和第一行:

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, ,

【问题讨论】:

  • 您应该发布示例 csv 数据,例如标题和第一行数据。
  • 请也显示异常(对于堆栈跟踪,仅包括相关行)。

标签: c# csv


【解决方案1】:

由于空格,您的字段名称和文件列标题不匹配。在 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();
}

【讨论】:

  • 这个选项已经不存在了。
  • 这里有 'csv.Configuration.IgnoreHeaderWhiteSpace' 选项!
  • 你现在必须使用PrepareHeaderForMatch函数:csv.Configuration.PrepareHeaderForMatch = (s, i) =&gt; s.Replace(" ", string.Empty);
【解决方案2】:

即使文档说明 CSVHelper 会自动将类映射到 CSV 文件,您仍然需要手动编写映射到 CSV 文件的映射。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 2018-06-29
    相关资源
    最近更新 更多