public ActionResult NhapFile(HttpPostedFileBase excelfile) { if (excelfile == null || excelfile.ContentLength == 0) { ViewBag.Error = "Bạn chưa tải file lên<br>"; return(View()); } else { if (excelfile.FileName.EndsWith("xls") || excelfile.FileName.EndsWith("xlsx")) { string path = Server.MapPath("~/assets/excel/" + excelfile.FileName); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } excelfile.SaveAs(path); //read data form excel Excel.Application application = new Excel.Application(); Excel.Workbook workbook = application.Workbooks.Open(path); Excel.Worksheet worksheet = workbook.ActiveSheet; Excel.Range range = worksheet.UsedRange; var listSV = new SinhVienDao(); for (int row = 2; row < range.Rows.Count; row++) { var sv = new SinhVien(); sv.MaSV = double.Parse(((Excel.Range)range.Cells[row, 1]).Text); sv.HoTen = ((Excel.Range)range.Cells[row, 2]).Text; sv.GioiTinh = ((Excel.Range)range.Cells[row, 3]).Text; sv.SoDT = ((Excel.Range)range.Cells[row, 4]).Text; sv.Email = ((Excel.Range)range.Cells[row, 5]).Text; sv.NgaySinh = DateTime.Now; sv.NoiSinh = ((Excel.Range)range.Cells[row, 7]).Text; sv.ChoTamTru = ((Excel.Range)range.Cells[row, 8]).Text; sv.ChucVu = ((Excel.Range)range.Cells[row, 9]).Text; var Lop = new LopDao(); sv.Id_Lop = Lop.lop(((Excel.Range)range.Cells[row, 10]).Text).Id_Lop; var nganh = new NganhDao(); sv.Id_Nganh = nganh.nganh(((Excel.Range)range.Cells[row, 11]).Text).Id_Nganh; listSV.create1(sv); } return(View()); } else { ViewBag.Error = "Bạn chưa tải file lên"; return(View()); } } return(View()); }
public ActionResult ThemSV(User us) { var lop = new LopDao(); var nganh = new NganhDao(); SetViewBag(lop.Id_lop_MacDinh()); SetViewBag2(nganh.Id_Nganh_MacDinh()); return(View()); }
public void SetViewBag2(int?selectedId = null) { var dao = new NganhDao(); ViewBag.Id_Nganh = new SelectList(dao.ListCurent(), "Id_Nganh", "TenNganh", selectedId); }