public IActionResult Questions_Insert(IFormCollection collection) { var l_id = collection["LectureId"]; var s_id = collection["SubjectId"]; if (collection["tf_true"].ToString() != null) { QuestionsCorrects questionsCorrects = new QuestionsCorrects(); questionsCorrects.AccountId = methods.get_Account_ID(); questionsCorrects.LectureId = Convert.ToInt32(l_id); questionsCorrects.SubjectId = Convert.ToInt32(s_id); questionsCorrects.Title = collection["title"]; questionsCorrects.QTrue = collection["tf_true"]; questionsCorrects.QFalse = collection["tf_false"]; questionsCorrects.Result = Convert.ToInt32(collection["tf_Result"]); questionsCorrects.Levels = Convert.ToInt16(collection["q_type"]); db.QuestionsCorrects.Add(questionsCorrects); } else { QuestionsTests test = new QuestionsTests(); test.AccountId = methods.get_Account_ID(); test.LectureId = Convert.ToInt32(l_id); test.SubjectId = Convert.ToInt32(s_id); test.Title = collection["title"]; test.A = collection["A"]; test.B = collection["B"]; test.C = collection["C"]; test.D = collection["D"]; test.E = collection["E"]; test.Result = Convert.ToInt32(collection["Result"]); //test.q_type = collection["q_type"]; db.QuestionsTests.Add(test); } db.SaveChanges(); ViewBag.ListOfLectures = get_List_Lectures(); return(View()); }
public ActionResult OnPostImport() { try { IFormFile file = Request.Form.Files[0]; string folderName = "Upload"; string webRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");// _hostingEnvironment.WebRootPath; string newPath = Path.Combine(webRootPath, folderName); StringBuilder sb = new StringBuilder(); if (!Directory.Exists(newPath)) { Directory.CreateDirectory(newPath); } if (file.Length > 0) { string sFileExtension = Path.GetExtension(file.FileName).ToLower(); ISheet sheet; string fullPath = Path.Combine(newPath, file.FileName); using (var stream = new FileStream(fullPath, FileMode.Create)) { file.CopyTo(stream); stream.Position = 0; if (sFileExtension == ".xls") { HSSFWorkbook hssfwb = new HSSFWorkbook(stream); //This will read the Excel 97-2000 formats sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook } else { XSSFWorkbook hssfwb = new XSSFWorkbook(stream); //This will read 2007 Excel format sheet = hssfwb.GetSheetAt(0); //get first sheet from workbook } IRow headerRow = sheet.GetRow(0); //Get Header Row int cellCount = headerRow.LastCellNum; sb.Append("<table class='mdl - data - table mdl - js - data - table mdl - data - table--selectable mdl - shadow--2dp'><tr>"); for (int j = 0; j < cellCount; j++) { NPOI.SS.UserModel.ICell cell = headerRow.GetCell(j); if (cell == null || string.IsNullOrWhiteSpace(cell.ToString())) { continue; } sb.Append("<th>" + cell.ToString() + "</th>"); } sb.Append("</tr>"); sb.AppendLine("<tr>"); for (int i = (sheet.FirstRowNum + 1); i <= sheet.LastRowNum; i++) //Read Excel File { IRow row = sheet.GetRow(i); if (row == null) { continue; } if (row.Cells.All(d => d.CellType == CellType.Blank)) { continue; } for (int j = row.FirstCellNum; j < cellCount; j++) { if (row.GetCell(j) != null) { sb.Append("<td>" + row.GetCell(j).ToString() + "</td>"); } } QuestionsTests test = new QuestionsTests(); test.LectureId = 1; test.SubjectId = 1; test.Title = row.GetCell(2).ToString(); test.A = row.GetCell(3).ToString(); test.B = row.GetCell(4).ToString(); test.C = row.GetCell(5).ToString(); test.D = row.GetCell(6).ToString(); test.E = row.GetCell(7).ToString(); test.Result = 1; test.Levels = 1; test.Status = 1; test.Type = 1; db.QuestionsTests.Add(test); db.SaveChanges(); sb.AppendLine("</tr>"); } sb.Append("</table>"); } } //Debug.WriteLine(sb.ToString()); ViewBag.table = sb.ToString(); return(this.Content(sb.ToString())); } catch { } return(null); }