Exemplo n.º 1
0
        public async Task <IActionResult> UploadFile(IFormFile uploadedFile)
        {
            if (uploadedFile.IsFileValid())
            {
                return(Content("File Not Selected"));
            }

            string fileExtension = Path.GetExtension(uploadedFile.FileName);

            if (fileExtension == ".xls" || fileExtension == ".xlsx")
            {
                var rootFolder   = $"{Directory.GetCurrentDirectory()}\\wwwroot\\Files";
                var fileLocation = await uploadedFile.GetFileLocationAndSaveFile(rootFolder);

                ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                using (ExcelPackage package = new ExcelPackage(fileLocation))
                {
                    ExcelWorksheet     workSheet   = package.Workbook.Worksheets[0];
                    int                totalRows   = workSheet.Dimension.Rows;
                    List <ProductList> productList = new List <ProductList>();
                    Guid               batch_guid  = Guid.NewGuid();


                    for (int i = 2; i <= totalRows; i++)
                    {
                        productList.Add(new ProductList
                        {
                            ID_BATCH_GUID  = batch_guid.ToString(),
                            TX_JAN         = workSheet.Cells[i, 1].Value != null ? workSheet.Cells[i, 1].Value.ToString() : string.Empty,
                            TX_SKU         = workSheet.Cells[i, 2].Value != null ? workSheet.Cells[i, 2].Value.ToString() : string.Empty,
                            TX_CATEGORY    = workSheet.Cells[i, 3].Value != null ? workSheet.Cells[i, 3].Value.ToString() : string.Empty,
                            TX_SUBCATEGORY = workSheet.Cells[i, 4].Value != null ? workSheet.Cells[i, 4].Value.ToString() : string.Empty,
                            TX_INGREDIENTS = workSheet.Cells[i, 5].Value != null ? workSheet.Cells[i, 5].Value.ToString() : string.Empty,
                            DT_UPLOADED    = DateTime.Now
                        });
                    }
                    List <CommonItems> commonItems = await this._commonItemsRepo.GetAll(this._appDBContext);

                    List <PermittedAdditive> permittedAdditives = await this._permittedAdditivesRepo.GetAll(this._appDBContext);

                    BusinessFacade bf = new BusinessFacade();
                    bf.ProcessUploadedFile(productList, commonItems, permittedAdditives);


                    if (await this._productRepo.Save(this._appDBContext, productList))
                    {
                        MemoryStream stream = new MemoryStream();
                        ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                        using (ExcelPackage pck = new ExcelPackage(stream))
                        {
                            List <object[]> test = new List <object[]>()
                            {
                                new object[] { "eleaz", "pogi" },
                                new object[] { "mead", "ganda" },
                            };
                            ExcelWorksheet ws = pck.Workbook.Worksheets.Add("MainView");
                            ws.Cells["A1"].LoadFromArrays(test);
                            var bytes = pck.GetAsByteArray();
                            return(File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "ExportFile.xlsx"));
                        }
                    }
                }
            }

            return(BadRequest());
        }