//Thêm File Excel protected void btnThem_Click(object sender, EventArgs e) { if (FileUpload2.HasFile) { if (Path.GetExtension(FileUpload2.FileName) == ".xlsx") { try { using (var excel = new ExcelPackage(FileUpload2.PostedFile.InputStream)) { //gọi bảng chứa dữ liệu excel ExcelPackage.LicenseContext = LicenseContext.Commercial; var ws = excel.Workbook.Worksheets.First(); int rowCount = ws.Dimension.End.Row; //get row count for (int row = 5; row <= rowCount; row++) { if (ws.Cells[row, 3].Text.Trim() == "") { break; } Sach sach = tbl_sach.GetByName(ws.Cells[row, 3].Text.Trim()); if (sach == null) { ThemNhomSach(ws.Cells[row, 5].Text.Trim()); ThemNhaXuatBan(ws.Cells[row, 8].Text.Trim()); sach = new Sach(); sach.MaSach = CreateKey(); sach.TenSach = ws.Cells[row, 3].Text.Trim(); sach.TacGia = ws.Cells[row, 4].Text.Trim(); sach.NhomSach = ws.Cells[row, 5].Text.Trim(); sach.GhiChu = ws.Cells[row, 6].Text.Trim(); sach.SoLuong = Int32.Parse(ws.Cells[row, 7].Text.Trim()); sach.NhaXuatBan = ws.Cells[row, 8].Text.Trim(); sach.NoiXuatBan = ws.Cells[row, 9].Text.Trim(); sach.NamXuatBan = ws.Cells[row, 10].Text.Trim(); if (ws.Cells[row, 11].Text.Trim() != "") { sach.NgayNhap = ws.Cells[row, 11].Text.Trim(); } else { sach.NgayNhap = DateTime.Now.ToString("dd/MM/yyyy"); } sach.TenFile = ""; tbl_sach.Insert(sach); } else { ThemNhomSach(sach.NhomSach); ThemNhaXuatBan(sach.NhaXuatBan); sach.TenSach = ws.Cells[row, 3].Text.Trim(); sach.TacGia = ws.Cells[row, 4].Text.Trim(); sach.NhomSach = ws.Cells[row, 5].Text.Trim(); sach.GhiChu = ws.Cells[row, 6].Text.Trim(); sach.SoLuong = Int32.Parse(ws.Cells[row, 7].Text.Trim()); sach.NhaXuatBan = ws.Cells[row, 8].Text.Trim(); sach.NoiXuatBan = ws.Cells[row, 9].Text.Trim(); sach.NamXuatBan = ws.Cells[row, 10].Text.Trim(); sach.NgayNhap = ws.Cells[row, 11].Text.Trim(); tbl_sach.Update(sach); } } } WebMsgBox.Show("Thêm Vào Thành Công"); } catch (Exception ex) { WebMsgBox.Show("Lỗi nhập file, hãy kiểm tra lại file của bạn"); } } else { WebMsgBox.Show("Lỗi Hệ thống: chỉ nhận file Excel"); } } else { WebMsgBox.Show("Hãy chọn tệp"); } ShowData(); }