/// <summary> /// 导入教师信息 /// </summary> /// <returns></returns> public ActionResult ImportTeacherInfo() { HttpPostedFileBase file = Request.Files["teacherInfo"]; filename = Path.GetFileName(file.FileName); string fileExtensionName = Path.GetExtension(file.FileName); string serverFileName = ExcelFileBusinessOperation.CreatePrefixion() + filename; string virtualPath = "/ExcelFiles/" + serverFileName; string savePath = Server.MapPath(virtualPath); if (fileExtensionName == ".xls" || fileExtensionName == ".xlsx" || fileExtensionName == ".xlsm") { try { file.SaveAs(savePath); List <Object> teachers = ExcelFileBusinessOperation.ExcelToList(savePath, "teacher", asmPathAndName, "Model.Teacher"); DbOperation.SetXmlPath(xmlPath); foreach (var teacher in teachers) { bool result; if (teacher is Teacher) { DbOperation.Save(teacher, "teacher", out result); User_dal.CreateNewUser(((Teacher)teacher).id, "老师"); } } } catch (Exception) { return(Json(new { tip = "导入教师信息出错" })); throw; } return(Json(new { tip = "成功导入教师信息" })); } else { string errorMessage = "请不要上传excel以外的文件"; return(Json(new { tip = errorMessage })); } }
/// <summary> /// 接收客户端上传的Excel文件并把Excel表格里的数据导入数据库相应的表 /// </summary> /// <returns></returns> public ActionResult ReceiveFile() { HttpPostedFileBase file = Request.Files["files"]; filename = Path.GetFileName(file.FileName); string fileExtensionName = Path.GetExtension(file.FileName); string serverFileName = ExcelFileBusinessOperation.CreatePrefixion() + filename; string virtualPath = "/ExcelFiles/" + serverFileName; string savePath = Server.MapPath(virtualPath); if (fileExtensionName == ".xls" || fileExtensionName == ".xlsx" || fileExtensionName == ".xlsm") { try { file.SaveAs(savePath); DbOperation.SetXmlPath(xmlPath); bool result; var students = ExcelFileBusinessOperation.ExcelToList(savePath, "Sheet1", asmPathAndName, "Model.Student"); foreach (var student in students) { if (student is Model.Student) { DbOperation.Save(student, "student", out result); User_dal.CreateNewUser(((Student)student).sno); } } } catch (Exception) { throw; } return(Json(new { tip = "导入学生信息成功" })); } else { string errorMessage = "请不要上传excel以外的文件"; return(Json(new { tip = errorMessage })); } }