コード例 #1
0
ファイル: CsvImport.cs プロジェクト: sjschukin/XDataConv
        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);
                }
            }
        }
コード例 #2
0
ファイル: CsvImport.cs プロジェクト: sjschukin/XDataConv
        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());
                }
        }
コード例 #3
0
        public override IEnumerable <T> LoadDataItems(SettingsMapCollection mapping, string filename)
        {
            var importedData = new List <T>();

            Logger.Info($"Opening file {filename} for import.");
            Errors.Clear();

            using (var stream = File.Open(filename, FileMode.Open, FileAccess.Read))
                using (var reader = ExcelReaderFactory.CreateReader(stream))
                {
                    // read the header
                    if (!reader.Read())
                    {
                        throw new ApplicationException("Файл пуст.");
                    }

                    Logger.Info("Reading a header.");

                    var headerNames = GetHeaderNames(reader);

                    Logger.Info("Checking if all need columns exist and getting columns ordinal.");

                    var activeMapItems      = mapping.GetActiveItems().ToArray();
                    var ordinal             = new Dictionary <string, int>();
                    var notFoundHeaderNames = new List <string>();

                    foreach (var mapItem in activeMapItems)
                    {
                        ordinal.Add(mapItem.Name, -1);

                        for (int i = 0; i < headerNames.Length; i++)
                        {
                            Logger.Debug($"Checking fo equal {mapItem.ImportFieldName} and {headerNames[i]}.");
                            if (!String.Equals(mapItem.ImportFieldName, headerNames[i], StringComparison.CurrentCultureIgnoreCase))
                            {
                                continue;
                            }

                            Logger.Debug($"Found {mapItem.Name} with ordinal {i}.");
                            ordinal[mapItem.Name] = i;
                            break;
                        }

                        if (ordinal[mapItem.Name] == -1)
                        {
                            Logger.Info($"The column {mapItem.ImportFieldName} not found.");
                            notFoundHeaderNames.Add(mapItem.ImportFieldName);
                        }
                    }

                    if (notFoundHeaderNames.Any())
                    {
                        throw new ApplicationException($"Отсутствуют необходимые колонки {String.Join(", ", notFoundHeaderNames)}. Импорт невозможен.");
                    }

                    Logger.Info("Checking columns complete. Start importing.");

                    // read the body
                    int lineNumber = 1;
                    int errorCount = 0;
                    var properties = activeMapItems
                                     .Select(mapItem => typeof(T).GetProperty(mapItem.Name))
                                     .ToArray();

                    while (errorCount <= ErrorCountLimit && reader.Read())
                    {
                        lineNumber++;
                        string currentFieldName = null;
                        var    dataItem         = new T {
                            RowId = lineNumber
                        };

                        try
                        {
                            foreach (var propertyInfo in properties)
                            {
                                currentFieldName = mapping[propertyInfo.Name].FieldName;
                                var value = reader[ordinal[propertyInfo.Name]];

                                if (value == null)
                                {
                                    continue;
                                }

                                var info = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ??
                                           propertyInfo.PropertyType;

                                if (info.Name == "String" && mapping[propertyInfo.Name].IsConvertImportToUpperCase)
                                {
                                    value = ((string)value).ToUpper(CultureInfo.InvariantCulture);
                                }
                                else
                                {
                                    value = Convert.ChangeType(value, info);
                                }

                                propertyInfo.SetValue(dataItem, value);
                            }
                        }
                        catch (Exception ex)
                        {
                            errorCount++;

                            Logger.Error($"Error occured during import file {filename}.", ex);
                            Errors.Add(new TError
                            {
                                RowId   = lineNumber,
                                Message = $"Ошибка при импорте поля {currentFieldName}. {ex.Message}"
                            });
                        }

                        importedData.Add(dataItem);
                    }
                }

            return(importedData.ToArray());
        }
コード例 #4
0
 public virtual IEnumerable <T> LoadDataItems(SettingsMapCollection mapping, string filename)
 {
     throw new NotImplementedException();
 }
コード例 #5
0
ファイル: DbfImport.cs プロジェクト: sjschukin/XDataConv
        public override IEnumerable <T> LoadDataItems(SettingsMapCollection mapping, string filename)
        {
            var importedData = new List <T>();
            int errorCount   = 0;

            Logger.Info($"Opening file {filename} for import.");
            Errors.Clear();

            using (var table = Table.Open(filename))
            {
                Logger.Info("Reading a header.");

                var headerNames = table.Columns.Select(item => item.Name).ToArray();

                Logger.Info("Checking if all need columns exist and getting columns ordinal.");

                var activeMapItems      = mapping.GetActiveItems().ToArray();
                var notFoundHeaderNames = new List <string>();

                foreach (var mapItem in activeMapItems)
                {
                    var isFound = headerNames.Any(item =>
                                                  item.Equals(mapItem.ImportFieldName, StringComparison.CurrentCultureIgnoreCase));

                    if (isFound)
                    {
                        continue;
                    }

                    Logger.Info($"The column {mapItem.ImportFieldName} not found.");
                    notFoundHeaderNames.Add(mapItem.ImportFieldName);
                }

                if (notFoundHeaderNames.Any())
                {
                    throw new ApplicationException($"Отсутствуют необходимые колонки {String.Join(", ", notFoundHeaderNames)}. Импорт невозможен.");
                }

                Logger.Info("Checking columns complete. Start importing.");

                int lineNumber = 0;
                var properties = activeMapItems
                                 .Select(mapItem => typeof(T).GetProperty(mapItem.Name))
                                 .ToArray();

                var reader = table.OpenReader(Encoding.GetEncoding(866));

                while (errorCount <= ErrorCountLimit && reader.Read())
                {
                    lineNumber++;
                    string currentFieldName = null;
                    var    dataItem         = new T {
                        RowId = lineNumber
                    };

                    try
                    {
                        foreach (var propertyInfo in properties)
                        {
                            currentFieldName = mapping[propertyInfo.Name].FieldName;
                            var value = reader.GetValue(currentFieldName);

                            if (value == null)
                            {
                                continue;
                            }

                            var info = Nullable.GetUnderlyingType(propertyInfo.PropertyType) ??
                                       propertyInfo.PropertyType;

                            if (info.Name == "String" && mapping[propertyInfo.Name].IsConvertImportToUpperCase)
                            {
                                value = ((string)value).ToUpper(CultureInfo.InvariantCulture);
                            }
                            else
                            {
                                value = Convert.ChangeType(value, info);
                            }

                            propertyInfo.SetValue(dataItem, value);
                        }
                    }
                    catch (Exception ex)
                    {
                        errorCount++;

                        Logger.Error($"Error occured during import file {filename}.", ex);
                        Errors.Add(new TError
                        {
                            RowId   = lineNumber,
                            Message = $"Ошибка при импорте поля {currentFieldName}. {ex.Message}."
                        });
                    }

                    importedData.Add(dataItem);
                }
            }

            return(importedData.ToArray());
        }