/// <summary> /// Create a new TN_ExamQuestion object. /// </summary> /// <param name="id">Initial value of the ID property.</param> /// <param name="examID">Initial value of the ExamID property.</param> /// <param name="questionID">Initial value of the QuestionID property.</param> public static TN_ExamQuestion CreateTN_ExamQuestion(global::System.Int32 id, global::System.Int32 examID, global::System.Int32 questionID) { TN_ExamQuestion tN_ExamQuestion = new TN_ExamQuestion(); tN_ExamQuestion.ID = id; tN_ExamQuestion.ExamID = examID; tN_ExamQuestion.QuestionID = questionID; return tN_ExamQuestion; }
/// <summary> /// Deprecated Method for adding a new object to the TN_ExamQuestion EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToTN_ExamQuestion(TN_ExamQuestion tN_ExamQuestion) { base.AddObject("TN_ExamQuestion", tN_ExamQuestion); }
public ActionResult CreateQuestionFromExcelFile(int examID, HttpPostedFileBase file) { try { if (file != null) { var fileUtil = new FileUtil(); string folderUpload = @"D:\FileUploads\"; if (!fileUtil.CheckDirectoryExists(folderUpload)) { fileUtil.CreateDirectory(folderUpload); } string fileName = "nganhangcauhoi" + file.FileName.Substring(file.FileName.LastIndexOf(".")); file.SaveAs(folderUpload + fileName); // đọc file excel và lưu vào csdl //Stream streamDictionary = GetResourceFileStream(folderUpload + fileName); using (var document = SpreadsheetDocument.Open(folderUpload + fileName, false)) { var workbookPart = document.WorkbookPart; var workbook = workbookPart.Workbook; var sheets = workbook.Descendants<Sheet>(); foreach (var sheet in sheets) { if (sheet.SheetId.HasValue && sheet.SheetId.ToString().Equals("1")) { #region Read excel var worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id); var sharedStringPart = workbookPart.SharedStringTablePart; var values = sharedStringPart.SharedStringTable.Elements<SharedStringItem>().ToArray(); var cells = worksheetPart.Worksheet.Descendants<Cell>(); var rows = worksheetPart.Worksheet.Descendants<Row>(); TN_Question questionTemp = new TN_Question(); int countDataNull = 0; bool flag = false; foreach (Row row in rows) { var question = new TN_Question(); var answer = new TN_Answer(); foreach (Cell c in row.Elements<Cell>()) { string value = string.Empty; if ((c.DataType != null) && (c.DataType == CellValues.SharedString)) { int ssid = int.Parse(c.CellValue.Text); value = sharedStringPart.SharedStringTable.ChildElements[ssid].InnerText; } else if (c.CellValue != null) { value = c.CellValue.Text; } // xử lý ban đầu value = value.TrimEnd('%'); if (c.CellReference == "A" + row.RowIndex) { if (!string.IsNullOrEmpty(value)) { question.Content = value; answer.Content = value; countDataNull = 0; } else { countDataNull++; break; } } if (c.CellReference == "B" + row.RowIndex) { if (!string.IsNullOrEmpty(value)) { answer.IsCorrect = true; } else { answer.IsCorrect = false; } } if (c.CellReference == "C" + row.RowIndex && !string.IsNullOrEmpty(value)) { if (!string.IsNullOrEmpty(value)) { question.Type = value; flag = true; } } } if (countDataNull == 0) { if (flag) { question.IsMultiChoose = false; db.TN_Question.AddObject(question); questionTemp = question; var questionExam = new TN_ExamQuestion() { ExamID = examID, QuestionID = question.ID }; db.TN_ExamQuestion.AddObject(questionExam); flag = false; } else { answer.QuestionID = questionTemp.ID; db.TN_Answer.AddObject(answer); } db.SaveChanges(); } } #endregion break; } } } ViewBag.Success = true; } } catch (Exception ex) { } return View(new List<TN_Exam>()); }