0

我有一个 .csv 文件,标题列在第一行。尽管我遵循 CsvHelper 主页 ( https://joshclose.github.io/CsvHelper/getting-started/ ) 中的示例代码,但从该文件读取值时出现问题。

我的问题:“没有标题记录可以按名称确定索引。”

我的代码有路径:

控制器:

      public void TestCSV()
      {
         try
         {
            using (var reader = new StreamReader(Server.MapPath("/Resource/DHLProject/DHLAttachedFiles/HANG NHAP.csv")))
            {
               string line = null;
               while ((line = reader.ReadLine()) != null)
               {
                  Console.WriteLine(line);
                  using (var csv = new CsvReader(reader, CultureInfo.InvariantCulture))
                  {
                     csv.Configuration.HasHeaderRecord = false;
                     csv.Configuration.HeaderValidated = null;
                     csv.Configuration.Delimiter = ",";
                     csv.Configuration.RegisterClassMap<DHL_ImportModelMap>();
                     csv.Read();
                     var record = csv.GetRecord<DHL_ImportModel>();
                  }
               }
            }
         }
         catch (Exception ex)
         {
         }
      }

模型:

   public class DHL_ImportModelMap : ClassMap<DHL_ImportModel>
   {
      public DHL_ImportModelMap()
      {
         Map(m => m.BillingAccount).Name("Billing Account");
         Map(m => m.BillingAccountName).Name("Billing Account Name");
         Map(m => m.VATNumber).Name("VAT Number");
         Map(m => m.ShipmentNumber).Name("Shipment Number");
         Map(m => m.ShipmentDate).Name("Shipment Date");
         Map(m => m.ProductName).Name("Product Name");
         Map(m => m.OriginalCountryCode).Name("Orig Country Code");
         Map(m => m.OriginalCountryName).Name("Orig Country Name");
         Map(m => m.Sender).Name("Senders Name");
         Map(m => m.DestinationCode).Name("Destination");
         Map(m => m.DestinationName).Name("Dest Name");
         Map(m => m.DestinationCountryCode).Name("Dest Country Code");
         Map(m => m.DestinationCountryName).Name("Dest Country Name");
         Map(m => m.ReceiversCompanyName).Name("Receivers Name");
         Map(m => m.ReceiversCompanyAddress1).Name("Receivers Address 1");
         Map(m => m.ReceiversCompanyAddress2).Name("Receivers Address 2");
         Map(m => m.ReceiversContact).Name("Receivers Contact");
         Map(m => m.Weight).Name("Weight (kg)");
         Map(m => m.Currency).Name("Currency");
         Map(m => m.Total_amount_non_VAT).Name("Total amount (excl. VAT)");
         Map(m => m.Total_amount_include_VAT).Name("Total amount (incl. VAT)");
      }
   }
   public class DHL_ImportModel
   {
      [Name("Billing Account")]
      public string BillingAccount { get; set; }
      [Name("Billing Account Name")]
      public string BillingAccountName { get; set; }
      [Name("VAT Number")]
      public string VATNumber { get; set; }
      [Name("Shipment Number")]
      public string ShipmentNumber { get; set; }
      [Name("Shipment Date")]
      public string ShipmentDate { get; set; }
      [Name("Product Name")]
      public string ProductName { get; set; }
      [Name("Orig Country Code")]
      public string OriginalCountryCode { get; set; }
      [Name("Orig Country Name")]
      public string OriginalCountryName { get; set; }
      [Name("Senders Name")]
      public string Sender { get; set; }
      [Name("Destination")]
      public string DestinationCode { get; set; }
      [Name("Dest Name")]
      public string DestinationName { get; set; }
      [Name("Dest Country Code")]
      public string DestinationCountryCode { get; set; }
      [Name("Dest Country Name")]
      public string DestinationCountryName { get; set; }
      [Name("Receivers Name")]
      public string ReceiversCompanyName { get; set; }
      [Name("Receivers Address 1")]
      public string ReceiversCompanyAddress1 { get; set; }
      [Name("Receivers Address 2")]
      public string ReceiversCompanyAddress2 { get; set; }
      [Name("Receivers Contact")]
      public string ReceiversContact { get; set; }
      [Name("Weight (kg)")]
      public string Weight { get; set; }
      [Name("Currency")]
      public string Currency { get; set; }
      [Name("Total amount (excl. VAT)")]
      public string Total_amount_non_VAT { get; set; }
      [Name("Total amount (incl. VAT)")]
      public string Total_amount_include_VAT { get; set; }

   }
4

1 回答 1

2

如果您的文件有标题记录,那么您应该删除

csv.Configuration.HasHeaderRecord = false;  --Remove this line

仅当您的文件在第一行中没有标题记录时才应使用此选项。

于 2020-10-12T12:02:35.770 回答