private static IImportService GetImporter(Properties properties, string method) { IImportService importer; var file = properties.SafeRead(PropertyConstants.File); var headerRowSpaces = properties.SafeRead(PropertyConstants.HeaderRowSpaces, "Remove"); switch (method) { case nameof(Method.ExcelBulkImport): case nameof(Method.ExcelBulkImportWithTransaction): case nameof(Method.ExcelImport): case nameof(Method.ExcelImportWithTransaction): var excelImportSettings = new ExcelImportSettings { File = file, SheetName = properties.SafeRead(PropertyConstants.SheetName), HeaderRowIndex = properties.SafeRead(PropertyConstants.HeaderRowIndex, 0), HeaderRowSpaces = headerRowSpaces, DuplicateDelimiter = properties.SafeRead(PropertyConstants.DuplicateColumnDelimiter, ";") }; importer = new ImportExcelFileService(excelImportSettings); break; case nameof(Method.CsvBulkImport): case nameof(Method.CsvBulkImportWithTransaction): case nameof(Method.CsvImport): case nameof(Method.CsvImportWithTransaction): var csvImportSettings = new CsvImportSettings { File = file, HeaderRowSpaces = headerRowSpaces, ColumnDelimiter = properties.SafeRead(PropertyConstants.ColumnDelimiter, ','), TextQualifier = properties.SafeRead(PropertyConstants.TextQualifier, '"') }; importer = new ImportCsvFileService(csvImportSettings); break; default: throw new Exception($"No importer found for '{method}'"); } return(importer); }
internal ImportExcelFileService(ExcelImportSettings settings) { _settings = settings; if (_settings.HeaderRowSpaces != "Replace" && _settings.HeaderRowSpaces != "Remove") { throw new ArgumentOutOfRangeException(nameof(_settings.HeaderRowSpaces), _settings.HeaderRowSpaces, "Header Row Spaces value must be either 'Replace' or 'Remove'"); } if (_settings.DuplicateDelimiter.ToCharArray().Length > 1) { throw new ArgumentOutOfRangeException(nameof(_settings.DuplicateDelimiter), _settings.DuplicateDelimiter, "Duplicate Column Delimiter is invalid."); } }
private static ImportStatus ImportExcel(string fileName) { var settings = new ExcelImportSettings { File = GetK2FileString(fileName), HeaderRowSpaces = "Replace", SheetName = "Data Sheet", HeaderRowIndex = 3 }; var importer = new ImportExcelFileService(settings); _importedData = importer.Results; Console.WriteLine(importer.Message); return(importer.Status); }