private void btnAdd_Click(object sender, RoutedEventArgs e) { if (fopen.FileName != "") { Excel.Application app = new Excel.Application(); Excel.Workbook wb = app.Workbooks.Open(fopen.FileName); Excel.Worksheet sheet = null; try { sheet = wb.Sheets[1]; Excel.Range range = sheet.UsedRange; int rows = range.Rows.Count; int col = 43; for (int i = 3; i <= rows; i++) { if (string.IsNullOrEmpty(range.Cells[i, col].Text.ToString()) != true) { if (string.IsNullOrEmpty(range.Cells[i, col + 1].Text.ToString()) != true) { if (string.IsNullOrEmpty(range.Cells[i, col + 6].Text.ToString()) != true) { if (string.IsNullOrEmpty(range.Cells[i, col + 7].Text.ToString()) != true) { if (string.IsNullOrEmpty(range.Cells[i, col + 20].Text.ToString()) != true && range.Cells[i, col + 20].Text.ToString() != "#NAME?" && range.Cells[i, col + 20].Text.ToString() != "#N/A") { if (string.IsNullOrEmpty(range.Cells[i, col + 21].Text.ToString()) != true) { if (string.IsNullOrEmpty(range.Cells[i, col + 23].Text.ToString()) != true && range.Cells[i, col + 23].Text.ToString() != "#NAME?" && range.Cells[i, col + 23].Text.ToString() != "#N/A") { DTO.NhanVienDTO nv = new DTO.NhanVienDTO(); manvLast = BUS.NhanVienBUS.GetLastNhanVien().Rows[0][0].ToString(); nv.MaNV = NextID(manvLast, "NV"); nv.HoTen = range.Cells[i, col].Text.ToString(); nv.NgayVaoLam = Convert.ToDateTime(range.Cells[i, col + 1].Text.ToString()); if (string.IsNullOrEmpty(range.Cells[i, col + 2].Text.ToString()) != true && range.Cells[i, col + 2].Text.ToString() != "#NAME?" && range.Cells[i, col + 2].Text.ToString() != "#N/A") { nv.MaPB = "'" + range.Cells[i, col + 2].Text.ToString() + "'"; } else { nv.MaPB = "NULL"; } nv.LuongCanBan = range.Cells[i, col + 24].Text.ToString(); if (string.IsNullOrEmpty(range.Cells[i, col + 3].Text.ToString()) != true) { nv.HinhAnh = range.Cells[i, col + 3].Text.ToString(); } else { nv.HinhAnh = ""; } if (string.IsNullOrEmpty(range.Cells[i, col + 4].Text.ToString()) != true && range.Cells[i, col + 4].Text.ToString() != "#NAME?" && range.Cells[i, col + 4].Text.ToString() != "#N/A") { nv.MaTT = Int16.Parse(range.Cells[i, col + 4].Text.ToString()); } else { nv.MaTT = 1; } DTO.ThongTinChiTietNhanVienDTO ttct = new DTO.ThongTinChiTietNhanVienDTO(); ttct.MaNV = nv.MaNV; if (string.IsNullOrEmpty(range.Cells[i, col + 5].Text.ToString()) != true && range.Cells[i, col + 5].Text.ToString() != "#NAME?" && range.Cells[i, col + 5].Text.ToString() != "#N/A") { ttct.MaGT = Convert.ToBoolean(Convert.ToInt16(range.Cells[i, col + 5].Text.ToString())); } else { ttct.MaGT = true; } ttct.CMND = range.Cells[i, col + 6].Text.ToString(); ttct.NgaySinh = Convert.ToDateTime(range.Cells[i, col + 7].Text.ToString()); if (string.IsNullOrEmpty(range.Cells[i, col + 8].Text.ToString()) != true) { ttct.NoiSinh = range.Cells[i, col + 8].Text.ToString(); } else { ttct.NoiSinh = ""; } if (string.IsNullOrEmpty(range.Cells[i, col + 9].Text.ToString()) != true) { ttct.DienThoai = range.Cells[i, col + 9].Text.ToString(); } else { ttct.DienThoai = ""; } if (string.IsNullOrEmpty(range.Cells[i, col + 10].Text.ToString()) != true) { ttct.SoNha = range.Cells[i, col + 10].Text.ToString(); } else { ttct.SoNha = ""; } if (string.IsNullOrEmpty(range.Cells[i, col + 11].Text.ToString()) != true) { ttct.Duong = range.Cells[i, col + 11].Text.ToString(); } else { ttct.Duong = ""; } if (string.IsNullOrEmpty(range.Cells[i, col + 12].Text.ToString()) != true) { ttct.PhuongXa = range.Cells[i, col + 12].Text.ToString(); } else { ttct.PhuongXa = ""; } if (string.IsNullOrEmpty(range.Cells[i, col + 13].Text.ToString()) != true && range.Cells[i, col + 13].Text.ToString() != "#NAME?" && range.Cells[i, col + 13].Text.ToString() != "#N/A") { ttct.QuanHuyen = "'" + range.Cells[i, col + 13].Text.ToString() + "'"; } else { ttct.QuanHuyen = "NULL"; } if (string.IsNullOrEmpty(range.Cells[i, col + 14].Text.ToString()) != true && range.Cells[i, col + 14].Text.ToString() != "#NAME?" && range.Cells[i, col + 14].Text.ToString() != "#N/A") { ttct.TinhTP = "'" + range.Cells[i, col + 14].Text.ToString() + "'"; } else { ttct.TinhTP = "NULL"; } if (string.IsNullOrEmpty(range.Cells[i, col + 15].Text.ToString()) != true && range.Cells[i, col + 15].Text.ToString() != "#NAME?" && range.Cells[i, col + 15].Text.ToString() != "#N/A") { ttct.QuocGia = "'" + range.Cells[i, col + 15].Text.ToString() + "'"; } else { ttct.QuocGia = "NULL"; } if (string.IsNullOrEmpty(range.Cells[i, col + 16].Text.ToString()) != true && range.Cells[i, col + 16].Text.ToString() != "#NAME?" && range.Cells[i, col + 16].Text.ToString() != "#N/A") { ttct.MaDT = "'" + range.Cells[i, col + 16].Text.ToString() + "'"; } else { ttct.MaDT = "NULL"; } if (string.IsNullOrEmpty(range.Cells[i, col + 17].Text.ToString()) != true && range.Cells[i, col + 17].Text.ToString() != "#NAME?" && range.Cells[i, col + 17].Text.ToString() != "#N/A") { ttct.MaTG = "'" + range.Cells[i, col + 17].Text.ToString() + "'"; } else { ttct.MaTG = "NULL"; } if (string.IsNullOrEmpty(range.Cells[i, col + 18].Text.ToString()) != true) { ttct.SoTheATM = range.Cells[i, col + 18].Text.ToString(); } else { ttct.SoTheATM = ""; } if (string.IsNullOrEmpty(range.Cells[i, col + 19].Text.ToString()) != true) { ttct.Email = range.Cells[i, col + 19].Text.ToString(); } else { ttct.Email = ""; } BUS.NguoiDungBUS.SetIsUpdated(Login.Account); BUS.NhanVienBUS.AddNhanVienMulti(nv, ttct); DTO.HopDongDTO hd = new DTO.HopDongDTO(); mahdLast = BUS.HopDongBUS.GetLastHopDong().Rows[0][0].ToString(); hd.MaHD = NextID(mahdLast, "HD"); hd.MaNV = nv.MaNV; hd.MaLoaiHD = Int16.Parse(range.Cells[i, col + 20].Text.ToString()); hd.NgayKyHD = Convert.ToDateTime(range.Cells[i, col + 21].Text.ToString()); if (string.IsNullOrEmpty(range.Cells[i, col + 22].Text.ToString()) != true) { hd.NgayHetHan = Convert.ToDateTime(range.Cells[i, col + 22].Text.ToString()); } else { switch (hd.MaLoaiHD.ToString()) { case "1": hd.NgayHetHan = Convert.ToDateTime("1/1/2500"); break; case "2": hd.NgayHetHan = Convert.ToDateTime(range.Cells[i, col + 21].Text.ToString()).AddYears(5).AddDays(-1); break; case "3": hd.NgayHetHan = Convert.ToDateTime(range.Cells[i, col + 21].Text.ToString()).AddYears(3).AddDays(-1); break; case "4": hd.NgayHetHan = Convert.ToDateTime(range.Cells[i, col + 21].Text.ToString()).AddYears(2).AddDays(-1); break; case "5": hd.NgayHetHan = Convert.ToDateTime(range.Cells[i, col + 21].Text.ToString()).AddYears(1).AddDays(-1); break; case "6": hd.NgayHetHan = Convert.ToDateTime(range.Cells[i, col + 21].Text.ToString()).AddMonths(3).AddDays(-1); break; } } hd.MaTTHD = Int16.Parse(range.Cells[i, col + 23].Text.ToString()); BUS.NguoiDungBUS.SetIsUpdated(Login.Account); BUS.HopDongBUS.AddHopDongMulti(hd); } else { string error = "Lỗi ở dòng " + i.ToString() + " cột Tình trạng hợp đồng."; listError.Add(error); } } else { string error = "Lỗi ở dòng " + i.ToString() + " cột Ngày ký hợp đồng."; listError.Add(error); } } else { string error = "Lỗi ở dòng " + i.ToString() + " cột Loại hợp đồng."; listError.Add(error); } } else { string error = "Lỗi ở dòng " + i.ToString() + " cột Ngày sinh."; listError.Add(error); } } else { string error = "Lỗi ở dòng " + i.ToString() + " cột CMND."; listError.Add(error); } } else { string error = "Lỗi ở dòng " + i.ToString() + " cột Ngày vào làm."; listError.Add(error); } } else { break; } } MessageBox.Show("Thêm nhân viên thành công!"); } catch (Exception ex) { MessageBox.Show(ex.Message, "Thông báo", MessageBoxButton.OK, MessageBoxImage.Error); } finally { wb = null; app.Quit(); } } }
private void btnAdd_Click(object sender, RoutedEventArgs e) { if (tbHoTen.Text != "") { if (tbCMND.Text.Length >= 9) { if (tbATM.Text.Length > 0) { if (tbLCB.Text != "") { if (tbEmail.Text != "") { if (IsMail(tbEmail.Text) == true) { if (dpNgaySinh.SelectedDate != null) { DTO.NhanVienDTO nv = new DTO.NhanVienDTO(); manvLast = BUS.NhanVienBUS.GetLastNhanVien().Rows[0][0].ToString(); nv.MaNV = NextID(manvLast, "NV"); nv.HoTen = tbHoTen.Text; nv.NgayVaoLam = Convert.ToDateTime(dpNVL.Text); //nv.MaCV = cbChucVu.SelectedValue.ToString(); nv.MaPB = cbPhongBan.SelectedValue.ToString(); nv.LuongCanBan = tbLCB.Text; if (cbHinhAnh.SelectedValue != null) { nv.HinhAnh = cbHinhAnh.SelectedValue.ToString(); } else { nv.HinhAnh = ""; } nv.MaTT = Int16.Parse(cbTinhTrang.SelectedValue.ToString()); DTO.ThongTinChiTietNhanVienDTO ttct = new DTO.ThongTinChiTietNhanVienDTO(); ttct.MaNV = nv.MaNV; ttct.MaGT = Convert.ToBoolean(cbGioiTinh.SelectedIndex); ttct.CMND = tbCMND.Text; ttct.NgaySinh = Convert.ToDateTime(dpNgaySinh.Text); ttct.NoiSinh = tbNoiSinh.Text; ttct.DienThoai = tbDienThoai.Text; ttct.SoNha = tbSoNha.Text; ttct.Duong = tbDuong.Text; ttct.PhuongXa = tbPhuongXa.Text; ttct.QuanHuyen = cbQuanHuyen.SelectedValue.ToString(); ttct.TinhTP = cbTinhTP.SelectedValue.ToString(); ttct.QuocGia = cbQuocGia.SelectedValue.ToString(); ttct.MaDT = cbDanToc.SelectedValue.ToString(); ttct.MaTG = cbTonGiao.SelectedValue.ToString(); ttct.SoTheATM = tbATM.Text; ttct.Email = tbEmail.Text; BUS.NguoiDungBUS.SetIsUpdated(Login.Account); BUS.NhanVienBUS.AddNhanVien(nv, ttct); DTO.HopDongDTO hd = new DTO.HopDongDTO(); mahdLast = BUS.HopDongBUS.GetLastHopDong().Rows[0][0].ToString(); hd.MaHD = NextID(mahdLast, "HD"); hd.MaNV = nv.MaNV; hd.MaLoaiHD = Int16.Parse(cbMaLoaiHD.SelectedValue.ToString()); hd.NgayKyHD = Convert.ToDateTime(dpNgayKyHD.Text); if (dpNgayHetHan.SelectedDate != null && dpNgayHetHan.Text != "") { hd.NgayHetHan = Convert.ToDateTime(dpNgayHetHan.Text); } else { hd.NgayHetHan = Convert.ToDateTime("1/1/2500"); } hd.MaTTHD = Int16.Parse(cbMaTTHD.SelectedValue.ToString()); BUS.NguoiDungBUS.SetIsUpdated(Login.Account); BUS.HopDongBUS.AddHopDong(hd); RefreshField(); } } else { dialogHostWarning.DataContext = "Chuỗi không đúng định dạng Email!"; dialogHostWarning.IsOpen = true; } } else { if (dpNgaySinh.SelectedDate != null) { DTO.NhanVienDTO nv = new DTO.NhanVienDTO(); manvLast = BUS.NhanVienBUS.GetLastNhanVien().Rows[0][0].ToString(); nv.MaNV = NextID(manvLast, "NV"); nv.HoTen = tbHoTen.Text; nv.NgayVaoLam = Convert.ToDateTime(dpNVL.Text); //nv.MaCV = cbChucVu.SelectedValue.ToString(); nv.MaPB = cbPhongBan.SelectedValue.ToString(); nv.LuongCanBan = tbLCB.Text; if (cbHinhAnh.SelectedValue != null) { nv.HinhAnh = cbHinhAnh.SelectedValue.ToString(); } else { nv.HinhAnh = ""; } nv.MaTT = Int16.Parse(cbTinhTrang.SelectedValue.ToString()); DTO.ThongTinChiTietNhanVienDTO ttct = new DTO.ThongTinChiTietNhanVienDTO(); ttct.MaNV = nv.MaNV; ttct.MaGT = Convert.ToBoolean(cbGioiTinh.SelectedIndex); ttct.CMND = tbCMND.Text; ttct.NgaySinh = Convert.ToDateTime(dpNgaySinh.Text); ttct.NoiSinh = tbNoiSinh.Text; ttct.DienThoai = tbDienThoai.Text; ttct.SoNha = tbSoNha.Text; ttct.Duong = tbDuong.Text; ttct.PhuongXa = tbPhuongXa.Text; ttct.QuanHuyen = cbQuanHuyen.SelectedValue.ToString(); ttct.TinhTP = cbTinhTP.SelectedValue.ToString(); ttct.QuocGia = cbQuocGia.SelectedValue.ToString(); ttct.MaDT = cbDanToc.SelectedValue.ToString(); ttct.MaTG = cbTonGiao.SelectedValue.ToString(); ttct.SoTheATM = tbATM.Text; ttct.Email = tbEmail.Text; BUS.NhanVienBUS.AddNhanVien(nv, ttct); DTO.HopDongDTO hd = new DTO.HopDongDTO(); mahdLast = BUS.HopDongBUS.GetLastHopDong().Rows[0][0].ToString(); hd.MaHD = NextID(mahdLast, "HD"); hd.MaNV = nv.MaNV; hd.MaLoaiHD = Int16.Parse(cbMaLoaiHD.SelectedValue.ToString()); hd.NgayKyHD = Convert.ToDateTime(dpNgayKyHD.Text); if (dpNgayHetHan.SelectedDate != null && dpNgayHetHan.Text != "") { hd.NgayHetHan = Convert.ToDateTime(dpNgayHetHan.Text); } else { hd.NgayHetHan = Convert.ToDateTime("1/1/2500"); } hd.MaTTHD = Int16.Parse(cbMaTTHD.SelectedValue.ToString()); BUS.HopDongBUS.AddHopDong(hd); RefreshField(); } else { dialogHostWarning.DataContext = "Hãy chọn Ngày Sinh!"; dialogHostWarning.IsOpen = true; } } } else { dialogHostWarning.DataContext = "Lương không được bỏ trống"; dialogHostWarning.IsOpen = true; } } } else { dialogHostWarning.DataContext = "CMND phải có 9 hoặc 12 số!"; dialogHostWarning.IsOpen = true; } } else { dialogHostWarning.DataContext = "Họ tên không được bỏ trống!"; dialogHostWarning.IsOpen = true; } }