public async Task <ActionResult> ImportAsync()
        {
            IFormFile file = Request.Form.Files[0];

            string folderName = "UploadExcel";

            string webRootPath = hostEnvironment.WebRootPath;

            string newPath = Path.Combine(webRootPath, folderName);

            var errorDictionary = new Dictionary <int, string>();

            if (!Directory.Exists(newPath))

            {
                Directory.CreateDirectory(newPath);
            }

            if (file.Length > 0)

            {
                string sFileExtension = Path.GetExtension(file.FileName).ToLower();

                ISheet sheet;

                string fullPath = Path.Combine(newPath, file.FileName);

                using (var stream = new FileStream(fullPath, FileMode.Create))

                {
                    file.CopyTo(stream);

                    stream.Position = 0;

                    if (sFileExtension == ".xls")

                    {
                        HSSFWorkbook hssfwb = new HSSFWorkbook(stream); //This will read the Excel 97-2000 formats

                        sheet = hssfwb.GetSheetAt(0);                   //get first sheet from workbook
                    }

                    else

                    {
                        XSSFWorkbook hssfwb = new XSSFWorkbook(stream); //This will read 2007 Excel format

                        sheet = hssfwb.GetSheetAt(0);                   //get first sheet from workbook
                    }

                    IRow headerRow = sheet.GetRow(0); //Get Header Row

                    int cellCount = headerRow.LastCellNum;

                    for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) //Read Excel File

                    {
                        IRow row = sheet.GetRow(i);

                        if (row == null)
                        {
                            continue;
                        }

                        if (row.Cells.All(d => d.CellType == CellType.Blank))
                        {
                            continue;
                        }

                        var newPharmacy = new PharmacyInputModel();

                        for (int j = row.FirstCellNum; j < cellCount; j++)

                        {
                            string currentRow = "";

                            if (row.GetCell(j) != null)
                            {
                                currentRow = row.GetCell(j).ToString().TrimEnd();
                            }

                            switch (j)
                            {
                            case 0:
                                if (numbersChecker.WholeNumberCheck(currentRow))
                                {
                                    newPharmacy.BrandexId = int.Parse(currentRow);
                                }
                                else
                                {
                                    errorDictionary[i] = currentRow;
                                }
                                break;

                            case 1:
                                break;

                            case 2:
                                if (currentRow == "")
                                {
                                    newPharmacy.PharmacyClass = PharmacyClass.Other;
                                }
                                else
                                {
                                    newPharmacy.PharmacyClass = (PharmacyClass)Enum.Parse(typeof(PharmacyClass), currentRow, true);
                                }
                                break;

                            case 3:
                                if (currentRow == "1")
                                {
                                    newPharmacy.Active = true;
                                }
                                else
                                {
                                    newPharmacy.Active = false;
                                }
                                break;

                            case 4:
                                int companyId = await this.companiesService.IdByName(currentRow);

                                if (companyId != 0)
                                {
                                    //int companyId = await this.companiesService.IdByName(currentRow);
                                    newPharmacy.CompanyId = companyId;
                                }
                                else
                                {
                                    errorDictionary[i] = currentRow;
                                }
                                break;

                            case 5:
                                newPharmacy.Name = currentRow;
                                break;

                            case 6:
                                int chainId = await this.pharmacyChainsService.IdByName(currentRow);

                                if (chainId != 0)
                                {
                                    newPharmacy.PharmacyChainId = chainId;
                                }
                                else
                                {
                                    errorDictionary[i] = currentRow;
                                }
                                break;

                            case 7:
                                newPharmacy.Address = currentRow;
                                break;

                            case 9:
                                int regionId = await this.regionsService.IdByName(currentRow);

                                if (regionId != 0)
                                {
                                    newPharmacy.RegionId = regionId;
                                }
                                else
                                {
                                    errorDictionary[i] = currentRow;
                                }
                                break;

                            case 10:
                                break;

                            case 11:
                                break;

                            case 12:
                                break;

                            case 13:
                                break;

                            case 14:
                                break;

                            case 15:
                                if (currentRow != "")
                                {
                                    if (numbersChecker.WholeNumberCheck(currentRow))
                                    {
                                        newPharmacy.PharmnetId = int.Parse(currentRow);
                                    }
                                }
                                break;

                            case 16:
                                if (currentRow != "")
                                {
                                    if (numbersChecker.WholeNumberCheck(currentRow))
                                    {
                                        newPharmacy.PhoenixId = int.Parse(currentRow);
                                    }
                                }
                                break;

                            case 17:
                                if (currentRow != "")
                                {
                                    if (numbersChecker.WholeNumberCheck(currentRow))
                                    {
                                        newPharmacy.SopharmaId = int.Parse(currentRow);
                                    }
                                }
                                break;

                            case 18:
                                if (currentRow != "")
                                {
                                    if (numbersChecker.WholeNumberCheck(currentRow))
                                    {
                                        newPharmacy.StingId = int.Parse(currentRow);
                                    }
                                }
                                break;

                            case 19:
                                break;

                            case 20:
                                break;

                            case 21:
                                int cityId = await this.citiesService.IdByName(currentRow);

                                if (cityId != 0)
                                {
                                    //int cityId = await this.citiesService.IdByName(currentRow);
                                    newPharmacy.CityId = cityId;
                                }
                                else
                                {
                                    errorDictionary[i] = currentRow;
                                }
                                break;
                            }
                        }

                        await this.pharmaciesService.CreatePharmacy(newPharmacy);
                    }
                }
            }

            var pharmacyErrorModel = new CustomErrorDictionaryOutputModel
            {
                Errors = errorDictionary
            };

            return(this.View(pharmacyErrorModel));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Import(IFormFile ImageFile)
        {
            IFormFile file = Request.Form.Files[0];

            string folderName = "UploadExcel";

            string webRootPath = hostEnvironment.WebRootPath;

            string newPath = Path.Combine(webRootPath, folderName);

            var errorDictionary = new Dictionary <int, string>();


            if (!Directory.Exists(newPath))

            {
                Directory.CreateDirectory(newPath);
            }

            if (file.Length > 0)

            {
                string sFileExtension = Path.GetExtension(file.FileName).ToLower();

                ISheet sheet;

                string fullPath = Path.Combine(newPath, file.FileName);

                using (var stream = new FileStream(fullPath, FileMode.Create))

                {
                    file.CopyTo(stream);

                    stream.Position = 0;

                    if (sFileExtension == ".xls")

                    {
                        HSSFWorkbook hssfwb = new HSSFWorkbook(stream); //This will read the Excel 97-2000 formats

                        sheet = hssfwb.GetSheetAt(0);                   //get first sheet from workbook
                    }

                    else

                    {
                        XSSFWorkbook hssfwb = new XSSFWorkbook(stream); //This will read 2007 Excel format

                        sheet = hssfwb.GetSheetAt(0);                   //get first sheet from workbook
                    }

                    IRow headerRow = sheet.GetRow(0); //Get Header Row

                    int cellCount = headerRow.LastCellNum;

                    for (int j = 0; j < cellCount; j++)
                    {
                        ICell cell = headerRow.GetCell(j);

                        if (cell == null || string.IsNullOrWhiteSpace(cell.ToString()))
                        {
                            continue;
                        }
                    }

                    for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) //Read Excel File

                    {
                        IRow row = sheet.GetRow(i);


                        for (int j = row.FirstCellNum; j < cellCount; j++)

                        {
                            string currentRow = "";

                            if (row.GetCell(j) != null)
                            {
                                currentRow = row.GetCell(j).ToString().TrimEnd();
                                await this.regionService.UploadRegion(currentRow);
                            }
                            else
                            {
                                errorDictionary[i] = currentRow;
                                continue;
                            }
                        }
                    }
                }
            }

            var citiesErrorModel = new CustomErrorDictionaryOutputModel
            {
                Errors = errorDictionary
            };

            return(this.View(citiesErrorModel));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> ImportAsync()
        {
            IFormFile file = Request.Form.Files[0];

            string folderName = "UploadExcel";

            string webRootPath = hostEnvironment.WebRootPath;

            string newPath = Path.Combine(webRootPath, folderName);

            var errorDictionary = new Dictionary <int, string>();

            if (!Directory.Exists(newPath))

            {
                Directory.CreateDirectory(newPath);
            }

            if (file.Length > 0)

            {
                string sFileExtension = Path.GetExtension(file.FileName).ToLower();

                ISheet sheet;

                string fullPath = Path.Combine(newPath, file.FileName);

                using (var stream = new FileStream(fullPath, FileMode.Create))

                {
                    file.CopyTo(stream);

                    stream.Position = 0;

                    if (sFileExtension == ".xls")

                    {
                        HSSFWorkbook hssfwb = new HSSFWorkbook(stream); //This will read the Excel 97-2000 formats

                        sheet = hssfwb.GetSheetAt(0);                   //get first sheet from workbook
                    }

                    else

                    {
                        XSSFWorkbook hssfwb = new XSSFWorkbook(stream); //This will read 2007 Excel format

                        sheet = hssfwb.GetSheetAt(0);                   //get first sheet from workbook
                    }

                    IRow headerRow = sheet.GetRow(0); //Get Header Row

                    int cellCount = headerRow.LastCellNum;

                    for (int j = 0; j < cellCount; j++)
                    {
                        ICell cell = headerRow.GetCell(j);

                        if (cell == null || string.IsNullOrWhiteSpace(cell.ToString()))
                        {
                            continue;
                        }
                    }

                    for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) //Read Excel File

                    {
                        IRow row = sheet.GetRow(i);

                        if (row == null)
                        {
                            continue;
                        }

                        if (row.Cells.All(d => d.CellType == CellType.Blank))
                        {
                            continue;
                        }

                        var newProduct = new ProductInputModel();

                        for (int j = row.FirstCellNum; j < cellCount; j++)

                        {
                            string currentRow = string.Empty;

                            if (row.GetCell(j) != null)
                            {
                                currentRow = row.GetCell(j).ToString().TrimEnd();
                            }

                            switch (j)
                            {
                            case 0:
                                if (currentRow != "")
                                {
                                    newProduct.Name = currentRow;
                                }
                                else
                                {
                                    errorDictionary[i] = currentRow;
                                }

                                break;

                            case 1:
                                if (currentRow != "")
                                {
                                    newProduct.ShortName = currentRow;
                                }
                                else
                                {
                                    errorDictionary[i] = currentRow;
                                }
                                break;

                            case 2:
                                if (this.numbersChecker.WholeNumberCheck(currentRow))
                                {
                                    newProduct.BrandexId = int.Parse(currentRow);
                                }
                                else
                                {
                                    errorDictionary[i] = currentRow;
                                }
                                break;

                            case 3:
                                if (currentRow != "")
                                {
                                    newProduct.PhoenixId = int.Parse(currentRow);
                                }
                                break;

                            case 4:
                                if (currentRow != "")
                                {
                                    newProduct.PharmnetId = int.Parse(currentRow);
                                }
                                break;

                            case 5:
                                if (currentRow != "")
                                {
                                    newProduct.StingId = int.Parse(currentRow);
                                }
                                break;

                            case 6:
                                if (currentRow != "")
                                {
                                    newProduct.SopharmaId = currentRow;
                                }
                                break;

                            case 7:
                                if (numbersChecker.NegativeNumberIncludedCheck(currentRow))
                                {
                                    newProduct.Price = double.Parse(currentRow);
                                }
                                else
                                {
                                    errorDictionary[i] = currentRow;
                                }
                                break;
                            }
                        }

                        await this.productsService.CreateProduct(newProduct);
                    }
                }
            }

            var productsErrorModel = new CustomErrorDictionaryOutputModel
            {
                Errors = errorDictionary
            };

            return(this.View(productsErrorModel));
        }