zoukankan      html  css  js  c++  java
  • 笔试在线考试系统-批量导入考生信息、课程管理

    今日任务:

    1、批量导入考生信息

     核心代码:

    视图:

    <div>
    @using (Html.BeginForm("ImportStudent", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
    <h2>
    批量导入考生信息
    </h2>
    <div>
    <fieldset id="myfieldset">
    <legend>excel模版格式 </legend><font color="red">导入考生信息的模板格式如下,若模板格式不正确,则不能导入!</font><br />
    <p style="color: Red; text-align: center;">@Html.ActionLink("下载模版", "GetFile")</p>
    </fieldset>
    </div>
    <div style="margin-top: 20px;">
    <fieldset id="myfieldset1">
    <legend>考生信息批量导入</legend>
    <p>
    @{
    IEnumerable<SelectListItem> list = ViewBag.Institution as IEnumerable<SelectListItem>;
    foreach (var item in list)
    {
    <span>@item.Text (编号: @item.Value)&nbsp;&nbsp;</span>
    }
    }
    <hr />
    </p>
    <p>
    选择文件:<input id="FileUpload" type="file" name="files" style=" 250px; height: 24px;
    background: White" class="easyui-validatebox" />
    </p>
    <p>
    <input id="btnImport" type="submit" value="导入" style=" 60px; height: 28px;" />
    </p>
    <p style="color: Red; text-align: center;">@ViewBag.error</p>
    </fieldset>
    </div>
    }
    </div>

    控制器:

    [HttpPost]
    public ActionResult ImportStudent(HttpPostedFileBase filebase)
    {
    HttpPostedFileBase file = Request.Files["files"];
    string FileName;
    string savePath;
    if (file == null || file.ContentLength <= 0)
    {
    ViewBag.error = "文件不能为空";
    return View();
    }
    else
    {
    string filename = Path.GetFileName(file.FileName);
    int filesize = file.ContentLength;//获取上传文件的大小单位为字节byte
    string fileEx = System.IO.Path.GetExtension(filename);//获取上传文件的扩展名
    string NoFileName = System.IO.Path.GetFileNameWithoutExtension(filename);//获取无扩展名的文件名
    int Maxsize = 4000 * 1024;//定义上传文件的最大空间大小为4M
    string FileType = ".xls,.xlsx";//定义上传文件的类型字符串

    FileName = NoFileName + DateTime.Now.ToString("yyyyMMddhhmmss") + fileEx;
    if (!FileType.Contains(fileEx))
    {
    ViewBag.error = "文件类型不对,只能导入xls和xlsx格式的文件";
    return View();
    }
    if (filesize >= Maxsize)
    {
    ViewBag.error = "上传文件超过4M,不能上传";
    return View();
    }
    string path = AppDomain.CurrentDomain.BaseDirectory + "/Content/uploads/excel/";
    savePath = Path.Combine(path, FileName);
    file.SaveAs(savePath);
    }

    //string result = string.Empty;
    string strConn;
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + savePath + ";" + "Extended Properties=Excel 8.0";
    OleDbConnection conn = new OleDbConnection(strConn);
    conn.Open();
    OleDbDataAdapter myCommand = new OleDbDataAdapter("select * from [Sheet1$]", strConn);
    DataSet myDataSet = new DataSet();
    try
    {
    myCommand.Fill(myDataSet, "ExcelInfo");
    }
    catch (Exception ex)
    {
    ViewBag.error = ex.Message;
    return View();
    }
    DataTable table = myDataSet.Tables["ExcelInfo"].DefaultView.ToTable();

    //引用事务机制,出错时,事物回滚
    using (TransactionScope transaction = new TransactionScope())
    {
    for (int i = 0; i < table.Rows.Count; i++)
    {
    //1:学号 2:姓名 4:校区
    StuInfo stu = new StuInfo();
    stu.stu_No = table.Rows[i][0].ToString();
    stu.stu_Name = table.Rows[i][1].ToString();
    stu.password = table.Rows[i][0].ToString();
    stu.ClassName= table.Rows[i][2].ToString();
    stu.institution_Id = Convert.ToInt32(table.Rows[i][3]);
    stu.LoginTimes = 0;
    stu.Status = 1;
    using (OnLineExamDB db = new OnLineExamDB())
    {
    db.StuInfo.Add(stu);
    db.SaveChanges();
    }
    }
    transaction.Complete();
    }
    ViewBag.error = "导入成功";
    System.Threading.Thread.Sleep(2000);
    return RedirectToAction("Index");
    }

    2、课程管理

    2.1课程列表

    页面效果:

    核心代码:

    public ActionResult CourseList()
    {
    List<CourseInfoListModel> list = GetAllCourseInfo().Select(a => new CourseInfoListModel
    {
    course_Id=a.course_Id,
    course_Name=a.course_Name
    }).ToList();
    return View(list);
    }

    2.2添加课程

    页面效果:

     核心代码:

    [HttpPost]
    public ActionResult AddCourse(AddCourseInfoModel model)
    {
    CourseInfo ci = new CourseInfo
    {
    course_Name=model.course_Name,
    status=1
    };
    using (OnLineExamDB db = new OnLineExamDB())
    {
    db.CourseInfo.Add(ci);
    db.SaveChanges();
    return RedirectToAction("CourseList");
    }
    }

    2.3删除课程(逻辑删除)

    页面效果:

    核心代码:

    [HttpPost]
    public ActionResult DelCourse(int id)
    {
    using (OnLineExamDB db = new OnLineExamDB())
    {
    CourseInfo model = db.CourseInfo.First(a => a.course_Id == id);
    model.status = 2;
    db.SaveChanges();
    return Json("ok");
    }
    }

    2.4修改课程信息

    页面效果:

    核心代码:

    public ActionResult UpdateCourse(int id)
    {
    using (OnLineExamDB db = new OnLineExamDB())
    {
    UpdateCourseInfoModel model = db.CourseInfo.Where(a => a.course_Id == id).Select(a => new UpdateCourseInfoModel
    {
    course_Id=a.course_Id,
    course_Name=a.course_Name
    }).FirstOrDefault();
    return View(model);
    }
    }

    [HttpPost]
    public ActionResult UpdateCourse(UpdateCourseInfoModel model)
    {
    using (OnLineExamDB db = new OnLineExamDB())
    {
    CourseInfo ci = db.CourseInfo.First(a => a.course_Id == model.course_Id);
    ci.course_Name = model.course_Name;
    db.SaveChanges();
    return View("CourseList");
    }
    }

    做的过程中暂无发现问题

  • 相关阅读:
    【持续更新】Android 源码下载地点
    android Notification 的使用
    如何设置 Notification 中PendingIntent 的 Intent
    CodeSmith部分类型转换代码
    【转】Request.ServerVariables参考
    ADT
    根据IP从纯真IP数据库查询地址
    VS2008重置默认环境
    Eclipse 安装SVN插件
    C#序列化JSON对象
  • 原文地址:https://www.cnblogs.com/chengyp/p/13331566.html
Copyright © 2011-2022 走看看