首先,谢谢你的时间。我正在尝试首次使用CsvHelper以及自定义类和自定义映射。我得到“在类型'System.Int32'和'System.String'之间没有定义强制运算符”
以下是我的课程:
class Vendor
{
public string VENDORID { get; set; }
public string VENDORNAME { get; set; }
public string VENDORSHORTNAME { get; set; }
public string VENDORCHECKNAME { get; set; }
public string HOLD { get; set; }
public string VENDORSTATUS { get; set; }
public string VENDORCLASSID { get; set; }
public string PRIMARYVENDORADDRESSID { get; set; }
public string VENDORCONTACT { get; set; }
public string ADDRESS1 { get; set; }
public string ADDRESS2 { get; set; }
public string ADDRESS3 { get; set; }
public string CITY { get; set; }
public string STATE { get; set; }
public string ZIPCODE { get; set; }
public string COUNTRYCODE { get; set; }
public string COUNTRY { get; set; }
public string PHONE1 { get; set; }
public string PHONE2 { get; set; }
public string PHONE3 { get; set; }
public string FAXNUMBER { get; set; }
public string VENDORACCOUNTNUMBER { get; set; }
public string PAYMENTTERMSID { get; set; }
public string TAXIDNUMBER { get; set; }
public string TAXREGISTRATIONNUMBER { get; set; }
public string USER_DEFINED1 { get; set; }
public string USER_DEFINED2 { get; set; }
public string TAX1099TYPE { get; set; }
public string TAX1099BOXNUMBER { get; set; }
public string PURCHASESACCOUNT { get; set; }
public string TRANSITROUTINGNUMBER { get; set; }
public string EFTBANKACCOUNTNUMBER { get; set; }
}
class VendorMap : CsvClassMap<Vendor>
{
public override void CreateMap()
{
Map(m => m.VENDORID).Name("Vendor ID");
Map(m => m.VENDORNAME).Name("Vendor name");
Map(m => m.VENDORSHORTNAME).Name("Vendor short name");
Map(m => m.VENDORCHECKNAME).Name("Vendor check name");
Map(m => m.HOLD).Name("Hold").Default(1);
Map(m => m.VENDORSTATUS).Name("Vendor status").Default("Active");
Map(m => m.VENDORCLASSID).Name("Vendor class ID");
Map(m => m.PRIMARYVENDORADDRESSID).Name("Primary vendor address ID");
Map(m => m.VENDORCONTACT).Name("Vendor contact");
Map(m => m.ADDRESS1).Name("Address 1");
Map(m => m.ADDRESS2).Name("Address 2");
Map(m => m.ADDRESS3).Name("Address 3");
Map(m => m.CITY).Name("City");
Map(m => m.STATE).Name("State");
Map(m => m.ZIPCODE).Name("Zip Code");
Map(m => m.COUNTRYCODE).Name("Country code");
Map(m => m.COUNTRY).Name("Country");
Map(m => m.PHONE1).Name("Phone 1");
Map(m => m.PHONE2).Name("Phone 2");
Map(m => m.PHONE3).Name("Phone 3");
Map(m => m.FAXNUMBER).Name("Fax number");
Map(m => m.VENDORACCOUNTNUMBER).Name("Vendor account number");
Map(m => m.PAYMENTTERMSID).Name("Payment terms ID");
Map(m => m.TAXIDNUMBER).Name("Tax ID number");
Map(m => m.TAXREGISTRATIONNUMBER).Name("Tax registration number");
Map(m => m.USER_DEFINED1).Name("User-defined 1");
Map(m => m.USER_DEFINED2).Name("User-defined 2");
Map(m => m.TAX1099TYPE).Name("Tax 1099 type:");
Map(m => m.TAX1099BOXNUMBER).Name("Tax 1099 box number");
Map(m => m.PURCHASESACCOUNT).Name("Purchases account");
Map(m => m.TRANSITROUTINGNUMBER).Name("Transit Routing Number");
Map(m => m.EFTBANKACCOUNTNUMBER).Name("EFT Bank Account Number");
}
}
这是我正在阅读文件的地方:
CsvReader csv = new CsvReader(reader);
csv.Configuration.HasHeaderRecord = true;
csv.Configuration.RegisterClassMap<VendorMap>();
List<Vendor> vendors = null;
try
{
vendors = csv.GetRecords<Vendor>().ToList();
理想情况下,我希望HOLD,TAX99TYPE和TAX99BOXNUMBER属性类型为“short”。但是,由此我收到错误“无法执行转换”。
以下是我的测试CSV的一部分:
"Vendor ID","Vendor name","Vendor short name","Vendor check name","Hold","Vendor status","Vendor clas
"AA0011","UAT Demo","","","","Active","","Residence","Demo UAT","TESTING","TESTING","","TESTING","KS"
"AA0011","UAT Demo","","","","Active","","Supply","Demo UAT","NEW ADDRESS","NEW ADDRESS","","NEW ADDR
答案 0 :(得分:2)
问题主要是由列定义(字符串)之间的类型不匹配引起的:
public string HOLD { get; set; }
并且它是默认值(Int32),
Map(m => m.HOLD).Name("Hold").Default(1);
据我所知,使用当前版本的CsvHelper,默认值类型应该与其相应列的类型定义匹配。
您有两种选择。将列HOLD定义为short
,或将默认值更改为字符串(即"1"
)。
假设您已经尝试过前一个选项,则错误“无法执行转换”可能意味着您的CSV文件中包含一个或多个列值
包含一个无法转换为short
的值(即字母数字值)。