private void ConfigureMap(CsvImportMap csvImportMap, SettingsMapCollection mapping) { csvImportMap.AutoMap(); csvImportMap.Map(item => item.RowId).Ignore().ConvertUsing(row => row.Context.RawRow - 2); foreach (var memberMap in csvImportMap.MemberMaps) { var mappingItem = mapping.FirstOrDefault(item => item.Name == memberMap.Data.Member.Name); if (mappingItem == null) { continue; } memberMap.Data.Names.Clear(); if (String.IsNullOrWhiteSpace(mappingItem.ImportFieldName)) { memberMap.Data.Ignore = true; } else { if (((PropertyInfo)memberMap.Data.Member).PropertyType == typeof(decimal) || ((PropertyInfo)memberMap.Data.Member).PropertyType == typeof(decimal?)) { memberMap.Data.TypeConverterOptions.CultureInfo = CultureInfo.GetCultureInfo("en-US"); } memberMap.Data.Names.Add(mappingItem.ImportFieldName); } } }
public override IEnumerable <T> LoadDataItems(SettingsMapCollection mapping, string filename) { Logger.Info($"Opening file {filename} for import."); Errors.Clear(); int errorCount = 0; using (var reader = new StreamReader(filename, Encoding.GetEncoding(1251))) using (var csv = new CsvReader(reader)) { var csvImportMap = new CsvImportMap(); ConfigureMap(csvImportMap, mapping); csv.Configuration.HasHeaderRecord = true; csv.Configuration.Delimiter = ";"; csv.Configuration.CultureInfo = CultureInfo.GetCultureInfo("ru-RU"); csv.Configuration.RegisterClassMap(csvImportMap); csv.Configuration.ReadingExceptionOccurred = ex => { errorCount++; if (errorCount > ErrorCountLimit) { return; } Logger.Error($"Error occured during import file {filename}.", ex); Errors.Add(new TError { RowId = ex.ReadingContext.RawRow, Message = $"Ошибка импорта строки. {ex.InnerException?.Message}" }); }; return(csv.GetRecords <T>().ToArray()); } }