1.今日任务
题库信息查询、创建以及信息修改
控制器对应代码:
1 public class LibraryController : Controller 2 { 3 // GET: Library 4 public ActionResult Index(int page = 1) 5 { 6 IPagedList list = LibraryService.GetList(page); 7 return View(list); 8 } 9 public ActionResult Add() 10 { 11 return View(); 12 } 13 14 public ActionResult Lead() 15 { 16 var list = LibraryService.GetAll(); 17 return View(list); 18 } 19 [HttpPost] 20 public ActionResult Add(string libraryname, string libraryremark) 21 { 22 Exam_Library library = new Exam_Library() 23 { 24 CreatTime = DateTime.Now, 25 UpdateTime = DateTime.Now, 26 Library_Remark = libraryremark, 27 Library_Name = libraryname, 28 LibraryStates = true 29 }; 30 try 31 { 32 int res = LibraryService.InsertLibrary(library); 33 } 34 catch (Exception ex) 35 { 36 return Json(new { msg = "添加失败" + ex, success = false }); 37 38 } 39 return Json(new { msg = "添加成功", success = true }); 40 } 41 public ActionResult Edit(int id) 42 { 43 var data = LibraryService.FindLibraryByID(id); 44 return View(data); 45 } 46 [HttpPost] 47 public ActionResult Edit(string libraryname, int id, string libraryremark) 48 { 49 Exam_Library library = new Exam_Library { Library_Name = libraryname, LibraryID = id, Library_Remark = libraryremark, UpdateTime = DateTime.Now }; 50 try 51 { 52 LibraryService.Update(library); 53 } 54 catch (Exception ex) 55 { 56 return Json(new { msg = "修改失败" + ex, success = false }); 57 58 } 59 return Json(new { msg = "修改成功", success = true }); 60 61 } 62 /// <summary> 63 /// 禁用题库 64 /// </summary> 65 /// <param name="id"></param> 66 /// <returns></returns> 67 public ActionResult Disable(int id) 68 { 69 try 70 { 71 int res = LibraryService.DisableLibrary(id); 72 } 73 catch (Exception ex) 74 { 75 return Json(new { msg = "禁用失败" + ex, success = false }); 76 77 } 78 return Json(new { msg = "禁用成功", success = true }); 79 80 } 81 }
Service层方法:
1 public class LibraryService 2 { 3 /// <summary> 4 /// 获取所有题库 5 /// </summary> 6 /// <param name="lmid"></param> 7 /// <param name="page"></param> 8 /// <returns></returns> 9 public static IPagedList GetList(int page = 1) 10 { 11 ExamSysDBContext db = new ExamSysDBContext(); 12 int pagesize = 10; 13 IPagedList list = db.Exam_Library.OrderBy(x => x.LibraryID).ToPagedList(page, pagesize); 14 return list; 15 } 16 public static List<Exam_Library> GetAll() 17 { 18 ExamSysDBContext db = new ExamSysDBContext(); 19 var list = db.Exam_Library.ToList(); 20 return list; 21 } 22 /// <summary> 23 /// 增加题库 24 /// </summary> 25 /// <param name="library"></param> 26 /// <returns></returns> 27 public static int InsertLibrary(Exam_Library library) 28 { 29 ExamSysDBContext dBContext = new ExamSysDBContext(); 30 dBContext.Exam_Library.Add(library); 31 return dBContext.SaveChanges(); 32 } 33 /// <summary> 34 /// 通过ID找到该题库 35 /// </summary> 36 /// <param name="id"></param> 37 /// <returns></returns> 38 public static Exam_Library FindLibraryByID(int id) 39 { 40 ExamSysDBContext dBContext = new ExamSysDBContext(); 41 var data = dBContext.Exam_Library.Where(x => x.LibraryID == id).FirstOrDefault(); 42 return data; 43 } 44 /// <summary> 45 /// 禁用题库 46 /// </summary> 47 /// <param name="id"></param> 48 /// <returns></returns> 49 public static int DisableLibrary(int id) 50 { 51 ExamSysDBContext dBContext = new ExamSysDBContext(); 52 53 var data = dBContext.Exam_Library.Where(x => x.LibraryID == id).FirstOrDefault(); 54 55 data.LibraryStates = false; 56 return dBContext.SaveChanges(); 57 } 58 /// <summary> 59 /// 修改题库名称 60 /// </summary> 61 /// <param name="library"></param> 62 /// <returns></returns> 63 public static int Update(Exam_Library library) 64 { 65 ExamSysDBContext dBContext = new ExamSysDBContext(); 66 var data = dBContext.Exam_Library.Where(x => x.LibraryID == library.LibraryID).FirstOrDefault(); 67 data.Library_Name = library.Library_Name; 68 data.Library_Remark = library.Library_Remark; 69 data.UpdateTime = library.UpdateTime; 70 71 return dBContext.SaveChanges(); 72 } 73 }
页面效果:
添加
修改
禁用
3.遇到问题:
(1).题库中的试题一条条添加效率比较低
4.解决方案
(1).使用Excel导入试题,格式必须与模板一致
(2).使用NPOI读取Excel信息
核心代码: Converter 类(获取Excel内容)
public static DataTable ExcelToDataSet(string filepath) { DataTable dt = new DataTable(); dt.Columns.Add("Title"); dt.Columns.Add("OptionA"); dt.Columns.Add("OptionB"); dt.Columns.Add("OptionC"); dt.Columns.Add("OptionD"); dt.Columns.Add("RightOption"); dt.Columns.Add("Analyze"); IWorkbook workbook = WorkbookFactory.Create(filepath); ISheet sheet = workbook.GetSheetAt(0);//获取第一个工作薄 //IRow row = (IRow)sheet.GetRow(0);//获取第一行 int Temp = 0; foreach (IRow item in sheet) { if(Temp!=0) { DataRow dr = dt.NewRow(); for (int i = 0; i < dt.Columns.Count; i++) { if(item.GetCell(i)!=null) { dr[i] = item.GetCell(i).ToString(); } else { dr[i] = ""; } } dt.Rows.Add(dr); } Temp = 1; } return dt; } }
控制器对应代码:
[HttpPost] public JavaScriptResult Lead(string libraryname, HttpPostedFileBase excelname) { try { if (excelname.ContentLength > 0) { var fileName = Path.GetFileName(excelname.FileName); var path = Path.Combine(Server.MapPath("~/Content/ExcelTemp"), fileName); excelname.SaveAs(path); DataTable dt = Utility.Converter.ExcelToDataSet(path); foreach (DataRow item in dt.Rows) { ///添加试题信息 拿到ID string title = item["Title"].ToString(); string answer = item["RightOption"].ToString(); string Analyze = item["Analyze"].ToString(); string OptionA = item["OptionA"].ToString(); string OptionB = item["OptionB"].ToString(); string OptionC = item["OptionC"].ToString(); string OptionD = item["OptionD"].ToString(); Exam_Question q = new Exam_Question { LibraryID = Convert.ToInt32(libraryname), QuestionAnswer = answer, QuestionDescribe = title, QuestionParse = Analyze, Score = 2 }; int id = Exam_QuestionService.Add(q); ///拿到ID继续添加选项 List<Exam_QuestionOptions> lists = new List<Exam_QuestionOptions>() { new Exam_QuestionOptions{ QuestionID=id, CreateTime=DateTime.Now, OptionCode="A", OptionDescribe=OptionA, UpdateTime=DateTime.Now }, new Exam_QuestionOptions{ QuestionID=id, CreateTime=DateTime.Now, OptionCode="B", OptionDescribe=OptionB, UpdateTime=DateTime.Now }, new Exam_QuestionOptions{ QuestionID=id, CreateTime=DateTime.Now, OptionCode="C", OptionDescribe=OptionC, UpdateTime=DateTime.Now }, new Exam_QuestionOptions{ QuestionID=id, CreateTime=DateTime.Now, OptionCode="D", OptionDescribe=OptionD, UpdateTime=DateTime.Now }, }; Exam_QuestionOptionsService.AddOptions(lists); } } } catch (Exception ex) { return JavaScript("<script>layer.msg('导入失败')</script>"); } return this.JavaScript("alert('导入成功')"); }
【注:从Excel导入题库必须与导入题库的模板格式一致 否则无法导入成功】
项目一周进度