public ActionResult FileUpload() { try { HttpPostedFileBase file = Request.Files["file"]; if (file != null) { string connectionString = ConfigurationManager.ConnectionStrings["IfoundDbContext"].ConnectionString; string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"), Path.GetFileName(file.FileName)); DataTable dt = _studentService.AddStudentByExcel(filePath, "Sheet1"); SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction); sqlbulkcopy.DestinationTableName = "Students";//数据库中的表名 sqlbulkcopy.WriteToServer(dt); return(RedirectToAction("Index")); } else { ViewData["AddExcelError"] = "Add Excel Error"; return(RedirectToAction("Index")); } } catch { return(RedirectToAction("Index")); } }