예제 #1
0
        public async Task <List <Vendor> > Import()
        {
            IFormFile formFile = Request.Form.Files[0];
            var       list     = new List <Vendor>();

            using (var stream = new MemoryStream())
            {
                await formFile.CopyToAsync(stream);

                ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                using (var package = new ExcelPackage(stream))
                {
                    try
                    {
                        ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
                        var            rowCount  = worksheet.Dimension.Rows;

                        for (int row = 2; row <= rowCount; row++)
                        {
                            list.Add(new Vendor
                            {
                                Name          = (worksheet.Cells[row, 1].Value).ToString(),
                                Address       = (worksheet.Cells[row, 2].Value).ToString(),
                                EmailId       = (worksheet.Cells[row, 3].Value).ToString(),
                                EmailId2      = (worksheet.Cells[row, 4].Value).ToString(),
                                ContactNo     = (worksheet.Cells[row, 5].Value).ToString(),
                                ContactPerson = (worksheet.Cells[row, 6].Value).ToString(),
                                CreatedDate   = DateTime.Now,
                                Status        = "1"
                            });
                        }
                        if (list.Count > 0)
                        {
                            var newUserIDs = list.Select(u => u.Name).Distinct().ToArray();
                            var usersInDb  = _dbContext.Vendor.Where(u => newUserIDs.Contains(u.Name) && u.Status.Equals("1"))
                                             .Select(u => u.Name).ToArray();
                            var usersNotInDb = list.Where(u => !usersInDb.Contains(u.Name));
                            foreach (Vendor user in usersNotInDb)
                            {
                                _dbContext.Add(user);
                                _dbContext.SaveChanges();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
            return(list);
        }
예제 #2
0
        public IActionResult Import()
        {
            IFormFile formFile = Request.Form.Files[0];
            var       list     = new List <ItemGroup>();

            using (var stream = new MemoryStream())
            {
                formFile.CopyToAsync(stream);
                ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
                using (var package = new ExcelPackage(stream))
                {
                    try
                    {
                        ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
                        var            rowCount  = worksheet.Dimension.Rows;

                        for (int row = 2; row <= rowCount; row++)
                        {
                            if (worksheet.Cells[row, 1].Value != null)
                            {
                                list.Add(new ItemGroup
                                {
                                    ItemGroupName     = (worksheet.Cells[row, 1].Value).ToString(),
                                    ParentItemGroupId = GetId((worksheet.Cells[row, 2].Value).ToString()).ToString(),
                                    CreatedDate       = DateTime.Now,
                                    Status            = "1"
                                });
                            }
                        }
                        if (list.Count > 0)
                        {
                            var newUserIDs = list.Select(u => u.ItemGroupName).Distinct().ToArray();
                            var usersInDb  = _dbContext.ItemGroup.Where(u => newUserIDs.Contains(u.ItemGroupName) && u.Status.Equals("1"))
                                             .Select(u => u.ItemGroupName).ToArray();
                            var usersNotInDb = list.Where(u => !usersInDb.Contains(u.ItemGroupName));
                            foreach (ItemGroup user in usersNotInDb)
                            {
                                //user.ParentItemGroupId = "0";
                                user.ItemGroupNLevelString = _dbContext.ItemGroup.Where(u => u.Id.ToString().Equals(user.ParentItemGroupId)).Select(s => s.ItemGroupName).FirstOrDefault();
                                _dbContext.Add(user);
                                _dbContext.SaveChanges();
                            }
                        }
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
            return(RedirectToAction("Index"));
        }
예제 #3
0
 public void Add <T>(T entity) where T : class
 {
     Context.Add(entity);
 }