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(SinhVien model) { var dao = new SinhVienDao(); if (dao.KiemTraTrungMaSV(model.MaSV) == 1) { dao.create1(model); SetViewBag(model.Id_Lop); SetViewBag2(model.Id_Nganh); ViewBag.Loi = "Thêm thành công"; return(View(dao.GetSV(model.MaSV))); } else { SetViewBag(model.Id_Lop); SetViewBag2(model.Id_Nganh); ViewBag.Loi = "Trùng mã sinh viên"; return(View()); } }