Ejemplo n.º 1
0
        private static Dictionary <string, string> GenerateOptions(exam_database q, QuestionType type)
        {
            var options = new Dictionary <string, string>();

            if (type == QuestionType.判断题)
            {
                options.Add("Y", "正确");
                options.Add("N", "错误");
                return(options);
            }
            int optionName = 65;

            if (!string.IsNullOrWhiteSpace(q.Text1))
            {
                options.Add(Convert.ToChar(optionName++).ToString(), q.Text1.Trim());
            }

            if (!string.IsNullOrWhiteSpace(q.Text2))
            {
                options.Add(Convert.ToChar(optionName++).ToString(), q.Text2.Trim());
            }

            if (!string.IsNullOrWhiteSpace(q.Text3))
            {
                options.Add(Convert.ToChar(optionName++).ToString(), q.Text3.Trim());
            }

            if (!string.IsNullOrWhiteSpace(q.Text4))
            {
                options.Add(Convert.ToChar(optionName++).ToString(), q.Text4.Trim());
            }

            if (!string.IsNullOrWhiteSpace(q.Text5))
            {
                options.Add(Convert.ToChar(optionName++).ToString(), q.Text5.Trim());
            }

            if (!string.IsNullOrWhiteSpace(q.Text6))
            {
                options.Add(Convert.ToChar(optionName).ToString(), q.Text6.Trim());
            }

            return(options);
        }
Ejemplo n.º 2
0
        public ActionResult Import(HttpPostedFileBase fileBase)
        {
            var file = Request.Files["files"];

            if (file == null || file.ContentLength <= 0)
            {
                ViewBag.Error = "文件不能为空";
                return(View());
            }

            int subjectId = int.Parse(Request.Form["SubjectId"]);
            int deptId    = int.Parse(Request.Form["DeptId"]);

            var          filename    = Path.GetFileName(file.FileName);
            var          fileEx      = Path.GetExtension(filename);                //获取上传文件的扩展名
            var          noFileName  = Path.GetFileNameWithoutExtension(filename); //获取无扩展名的文件名
            const string fileType    = ".xls,.xlsx";                               //定义上传文件的类型字符串
            var          newFileName = noFileName + DateTime.Now.ToString("yyyyMMddhhmmss") + fileEx;

            if (fileEx != null && !fileType.Contains(fileEx))
            {
                ViewBag.Error = "文件类型不对,只能导入xls和xlsx格式的文件";
                return(View());
            }
            string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/excel/";

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            string savePath = Path.Combine(path, newFileName);

            file.SaveAs(savePath);

            var expImp = new ExportImport();
            var dt     = expImp.GetFillData(savePath, false);

            if (dt == null)
            {
                ViewBag.Error = "导入的数据文件格式不正确";
                return(View());
            }
            var importCount = dt.Rows.Count;

            if (importCount == 0)
            {
                ViewBag.Error = "导入的数据文件为空";
                return(View());
            }

            string message;

            if (!CheckExcel(ref dt, out message))
            {
                ViewBag.Error = "!!!导入失败!!!<br/>" + message;
                return(View());
            }

            if (dt.Rows.Count == 0)
            {
                ViewBag.Error = "无正确的数据可导入";
                return(View());
            }

            var examDb = new List <exam_database>();


            foreach (DataRow dr in dt.Rows)
            {
                var exam = new exam_database()
                {
                    Type      = dr[0].ToString(),
                    Question  = dr[1].ToString(),
                    Text1     = dr[2].ToString(),
                    Text2     = dr[3].ToString(),
                    Text3     = dr[4].ToString(),
                    Text4     = dr[5].ToString(),
                    Text5     = dr[6].ToString(),
                    Text6     = dr[7].ToString(),
                    Answer    = dr[8].ToString().ToUpper(),
                    SubjectId = subjectId,
                    DeptId    = deptId
                };
                examDb.Add(exam);
            }

            //写库
            bool ret = _examRep.Add(examDb) > 0;

            ViewBag.Error = ret ? "导入成功" : "导入失败";

            return(View());
        }