public ImportModel ImportData(FileStream dataStream, ImportParametersViewModel parameters) { var result = new ImportModel(); var xssfwb = new XSSFWorkbook(dataStream); var sheet = xssfwb.GetSheetAt(0); var headers = sheet.GetRow(0); var errorWb = new XSSFWorkbook(); var errorSheet = errorWb.CreateSheet(ImportConstants.ErrorSheetName); var errorHeadersRow = errorSheet.CreateRow(0); for (int i = 0; i < headers.LastCellNum; i++) { errorHeadersRow.CreateCell(i).SetCellValue(headers.GetCell(i).ToString()); } errorHeadersRow.CreateCell(headers.LastCellNum).SetCellValue(ImportConstants.ErrorHeader); for (int i = 1; i <= sheet.LastRowNum; i++) { var currentRow = sheet.GetRow(i); var parsedRow = ParseRow(currentRow, headers, parameters); if (!parsedRow.ThereIsError) { result.Data.Add(parsedRow.RowData); } else { var errorRow = errorSheet.CreateRow(errorSheet.LastRowNum + 1); for (int j = 0; j < errorHeadersRow.LastCellNum; j++) { var errorHeader = errorHeadersRow.GetCell(j).ToString(); var errorCellValue = parsedRow.RowData[errorHeader]; errorRow.CreateCell(j).SetCellValue(errorCellValue); } } } if (errorSheet.LastRowNum > -1) { var fileExtension = Path.GetExtension(dataStream.Name); var fileName = dataStream.Name.Insert(dataStream.Name.IndexOf(fileExtension), ImportConstants.ErrorFile); var errorFilePath = Path.Combine(CommonConstants.TempFolder, ImportConstants.ErrorFolder, fileName); result.ErrorFilePath = errorFilePath; result.ErrorsCount = errorSheet.LastRowNum; using (var errorFile = new FileStream(errorFilePath, FileMode.Create, FileAccess.Write)) { errorWb.Write(errorFile); } } return(result); }
private ParseRowResult ParseRow(IRow row, IRow headers, ImportParametersViewModel parameters) { var parseResult = new ParseRowResult(); for (short j = 0; j < headers.LastCellNum; j++) { var header = headers.GetCell(j).ToString(); var currentValue = row.GetCell(j).ToString(); if (parseResult.ThereIsError) { parseResult.RowData.Add(header, currentValue); continue; } var headerParams = parameters.HeaderParameters.FirstOrDefault(h => header.Equals(h.HeaderName)); var parsedCell = ParseCellValue(currentValue, parameters.Parameters, headerParams); if (parsedCell.Success) { parseResult.RowData.Add(header, parsedCell.ResultValue); } else { parseResult.RowData.Add(header, currentValue); parseResult.ThereIsError = true; parseResult.ErrorMessage = parsedCell.ErrorMessage; } } if (parseResult.ThereIsError) { parseResult.RowData.Add(ImportConstants.ErrorHeader, parseResult.ErrorMessage); } return(parseResult); }
public ImportResult ProcessImport(FileStream dataStream, ImportParametersViewModel parameters, string userId) { var user = UserService.GetUserById(userId); if (user == null) { throw new Exception("Не вдалось завантажити дані користувача."); } SetImportStrategy(parameters.ImportMethod); var parsedImport = ImportStrategy.ImportData(dataStream, parameters); var currentImport = ImportRepository.SaveImport(new UserImport { User = user, FileName = new FileInfo(dataStream.Name).Name, RecordsCount = parsedImport.Data.Count + parsedImport.ErrorsCount, ErrorsCount = parsedImport.ErrorsCount, ImportDate = DateTime.Now }); ImportRepository.SaveImportParameters(ParseImportParameters(currentImport, parameters.Parameters)); ImportRepository.SaveImportHeaders(parameters.HeaderParameters.Select(hp => new ImportHeader { Import = currentImport, HeaderName = hp.HeaderName, DataType = ImportRepository.LoadDataTypeById((int)hp.DataType), PriorityLevel = ImportRepository.LoadPriorityLevelByPriority((int)hp.PriorityLevel) }).ToList()); var importHeaders = ImportRepository.LoadImportHeadersByImportId(currentImport.Id); var importData = new List <ImportData>(); foreach (var dataRow in parsedImport.Data) { var rowCells = new List <ImportData>(); foreach (var header in importHeaders) { if (dataRow.ContainsKey(header.HeaderName)) { rowCells.Add(new ImportData { Header = header, RowNumber = parsedImport.Data.IndexOf(dataRow) + 1, Value = dataRow[header.HeaderName] }); } } importData.AddRange(rowCells); } ImportRepository.SaveImportData(importData); return(new ImportResult { Result = OperationResult.Success, ErrorFilePath = parsedImport.ErrorFilePath, Message = $"Опрацьовано записів: {currentImport.RecordsCount}. З них не розпізнано: {parsedImport.ErrorsCount}" }); }