Exemplo n.º 1
0
        public async Task <IActionResult> UploadAgentData(List <IFormFile> files)
        {
            var filePath = Path.GetTempFileName();

            if (files.Count == 1 && files[0].Length > 0 && (files[0].FileName.ToUpper().EndsWith(".XLS") || files[0].FileName.ToUpper().EndsWith(".XLSX")))
            {
                using (var stream = new FileStream(filePath, FileMode.Create))
                {
                    await files[0].CopyToAsync(stream);
                }
                using (ExcelFile file = new ExcelFile(filePath))
                {
                    _context.Agents.RemoveRange(_context.Agents);
                    var agents = file.AsEntityList <Agent>(x => x.Replace(" ", string.Empty).ToUpper());
                    agents.DistinctBy(x => x.Username).ToList().ForEach(x => _context.Agents.Add(x));
                    await _context.SaveChangesAsync();
                }
                System.IO.File.Delete(filePath);
            }
            return(RedirectToAction("Index"));
        }