public ActionResult Import(HttpPostedFileBase excelfile) { Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); if (excelfile == null || excelfile.ContentLength == 0) { ViewBag.Error = "Файл не выбран! <br>"; return(View("Index", db.AspNetUsers.ToList())); } else { if (excelfile.FileName.EndsWith("xls") || excelfile.FileName.EndsWith("xlsx")) { db = new TTCEntities(); string path = Server.MapPath("~/Import/" + excelfile.FileName); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } excelfile.SaveAs(path); //Читаем из файла // Excel.Application ap = new Excel.Application(); Excel.Application application = new Excel.Application(); Excel.Workbook workbook = application.Workbooks.Open(path); Excel.Worksheet worksheet = workbook.ActiveSheet; Excel.Range range = worksheet.UsedRange; List <Machinist> listUsers = new List <Machinist>(); for (int row = 2; row <= range.Rows.Count; row++) { Machinist user = new Machinist(); user.FIO = ((Excel.Range)range.Cells[row, 1]).Text; user.Address = ((Excel.Range)range.Cells[row, 2]).Text; user.Telephone = ((Excel.Range)range.Cells[row, 3]).Text; db.Machinist.Add(user); db.SaveChanges(); } workbook.Close(); ViewBag.Error = "Данные загружены <br>"; return(View("Index", db.Machinist.ToList())); } else { ViewBag.Error = "Это не Excel! <br>"; return(View("Index", db.Machinist.ToList())); } } }