Esempio n. 1
0
        public void ImportMapping(int fileID)
        {
            var file = UploadFileRepository.Get().Where(m => m.ID == fileID && m.IsActive == true && m.IsImported == false).FirstOrDefault();

            if (file != null)
            {
                using (MemoryStream ms = new MemoryStream(file.UploadFileData))
                {
                    using (ExcelPackage excel = new ExcelPackage(ms))
                    {
                        if (excel.Workbook.Worksheets.Count == 0)
                        {
                            throw new Exception("File Is Empty");
                        }
                        else
                        {
                            try
                            {
                                List <FileMappingViewModel> fileMapping = new List <FileMappingViewModel>();
                                var worksheet = excel.Workbook.Worksheets.FirstOrDefault();
                                var start     = worksheet.Dimension.Start;
                                var end       = worksheet.Dimension.End;
                                for (int row = start.Row + 1; row <= end.Row; row++)
                                {
                                    fileMapping.Add(new FileMappingViewModel()
                                    {
                                        SheetName      = worksheet.Cells[row, 1].Text,
                                        VariableName   = worksheet.Cells[row, 2].Text,
                                        Cell           = worksheet.Cells[row, 3].Text,
                                        FileTypeID     = file.UploadFileTypeID,
                                        VariableTypeID = (int)VariableTypes.Nvarchar
                                    });
                                }
                                if (fileMapping.Count == 0)
                                {
                                    throw new Exception("File Is not Correct");
                                }

                                FileMappingRepository.ImportNewMappingData(fileMapping.Select(m => Transformer.AsDatabaseModel(m)).ToList(), file.UploadFileTypeID);
                                UploadFileRepository.CheckAsImported(file.ID);
                            }
                            catch (Exception ex)
                            {
                                throw new Exception(ex.Message);
                            }
                        }
                    }
                }
            }

            else
            {
                throw new Exception("File Not Found! It's already deactivated or imported!");
            }
        }