private void LoadData(List <EmployeeEntities> Emp = null) { try { dgvListEmp.Rows.Clear(); if (LoginInfo.Role == 0) { var items = Emp == null?EmpPro.GetAllData() as List <EmployeeEntities> : Emp; int index = 0; dgvListEmp.ColumnCount = 7; foreach (var item in items) { dgvListEmp.Rows.Add(); dgvListEmp.Rows[index].Cells[0].Value = item.ID; dgvListEmp.Rows[index].Cells[1].Value = item.Name; dgvListEmp.Rows[index].Cells[2].Value = item.Birthday.ToString("dd/MM/yyyy"); dgvListEmp.Rows[index].Cells[3].Value = item.Address; dgvListEmp.Rows[index].Cells[4].Value = item.Phone; dgvListEmp.Rows[index].Cells[5].Value = item.Account; //dgvListEmp.Rows[index].Cells[6].Value = item.Password; index++; } } else { var item = EmpPro.GetByID(LoginInfo.ID); int index = 0; dgvListEmp.ColumnCount = 7; dgvListEmp.Rows.Add(); dgvListEmp.Rows[index].Cells[0].Value = item.ID; dgvListEmp.Rows[index].Cells[1].Value = item.Name; dgvListEmp.Rows[index].Cells[2].Value = item.Birthday.ToString("dd/MM/yyyy"); dgvListEmp.Rows[index].Cells[3].Value = item.Address; dgvListEmp.Rows[index].Cells[4].Value = item.Phone; dgvListEmp.Rows[index].Cells[5].Value = item.Account; //dgvListEmp.Rows[index].Cells[6].Value = item.Password; index++; } } catch (Exception ex) { MessageBox.Show(ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void LoadData(List <EmployeeEntities> Emp = null) { dgvListEmp.Rows.Clear(); var items = Emp == null?EmpPro.GetAllData() as List <EmployeeEntities> : Emp; int index = 0; dgvListEmp.ColumnCount = 5; foreach (var item in items) { dgvListEmp.Rows.Add(); dgvListEmp.Rows[index].Cells[0].Value = item.ID; dgvListEmp.Rows[index].Cells[1].Value = item.Name; dgvListEmp.Rows[index].Cells[2].Value = item.Birthday.ToString("dd/MM/yyyy"); dgvListEmp.Rows[index].Cells[3].Value = item.Address; dgvListEmp.Rows[index].Cells[4].Value = item.Phone; index++; } }
private void btnExport_Click(object sender, EventArgs e) { string filePath = ""; // tạo SaveFileDialog để lưu file excel SaveFileDialog dialog = new SaveFileDialog(); // chỉ lọc ra các file có định dạng Excel dialog.Filter = "Excel | *.xlsx | Excel 2003 | *.xls"; // Nếu mở file và chọn nơi lưu file thành công sẽ lưu đường dẫn lại dùng //if (dialog.ShowDialog()) //{ // filePath = dialog.FileName; //} var a = dialog.ShowDialog(); if (a == DialogResult.OK) { filePath = dialog.FileName; } // nếu đường dẫn null hoặc rỗng thì báo không hợp lệ và return hàm if (string.IsNullOrEmpty(filePath)) { //MessageBox.Show("Đường dẫn báo cáo không hợp lệ"); return; } try { // If you use EPPlus in a noncommercial context // according to the Polyform Noncommercial license: //ExcelPackage.LicenseContext = LicenseContext.NonCommercial; using (ExcelPackage p = new ExcelPackage()) { // đặt tên người tạo file p.Workbook.Properties.Author = "Kteam by K9"; // đặt tiêu đề cho file p.Workbook.Properties.Title = "Báo cáo thống kê"; //Tạo một sheet để làm việc trên đó p.Workbook.Worksheets.Add("Kteam sheet"); // lấy sheet vừa add ra để thao tác ExcelWorksheet ws = p.Workbook.Worksheets[1]; // đặt tên cho sheet ws.Name = "Báo cáo thống kê số nhân viên"; // fontsize mặc định cho cả sheet ws.Cells.Style.Font.Size = 11; // font family mặc định cho cả sheet ws.Cells.Style.Font.Name = "Calibri"; // Tạo danh sách các column header string[] arrColumnHeader = { "Họ tên", "Năm sinh" }; // lấy ra số lượng cột cần dùng dựa vào số lượng header var countColHeader = arrColumnHeader.Count(); // merge các column lại từ column 1 đến số column header // gán giá trị cho cell vừa merge là Thống kê thông tni User Kteam ws.Cells[1, 1].Value = "Thống kê thông tin nhân viên"; ws.Cells[1, 1, 1, countColHeader].Merge = true; // in đậm ws.Cells[1, 1, 1, countColHeader].Style.Font.Bold = true; // căn giữa ws.Cells[1, 1, 1, countColHeader].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; int colIndex = 1; int rowIndex = 2; //tạo các header từ column header đã tạo từ bên trên foreach (var item in arrColumnHeader) { var cell = ws.Cells[rowIndex, colIndex]; //set màu thành gray var fill = cell.Style.Fill; fill.PatternType = ExcelFillStyle.Solid; fill.BackgroundColor.SetColor(System.Drawing.Color.LightBlue); //căn chỉnh các border var border = cell.Style.Border; border.Bottom.Style = border.Top.Style = border.Left.Style = border.Right.Style = ExcelBorderStyle.Thin; //gán giá trị cell.Value = item; colIndex++; } // lấy ra danh sách UserInfo từ ItemSource của DataGrid //List<UserInfo> userList = dtgExcel.ItemsSource.Cast<UserInfo>().ToList(); List <EmployeeEntities> Emp = EmpPro.GetAllData(); // với mỗi item trong danh sách sẽ ghi trên 1 dòng foreach (var item in Emp) { // bắt đầu ghi từ cột 1. Excel bắt đầu từ 1 không phải từ 0 colIndex = 1; // rowIndex tương ứng từng dòng dữ liệu rowIndex++; //gán giá trị cho từng cell ws.Cells[rowIndex, colIndex++].Value = item.Name; // lưu ý phải .ToShortDateString để dữ liệu khi in ra Excel là ngày như ta vẫn thấy.Nếu không sẽ ra tổng số :v ws.Cells[rowIndex, colIndex++].Value = item.Birthday.ToString("dd-MM-yyyy"); } //Lưu file lại Byte[] bin = p.GetAsByteArray(); File.WriteAllBytes(filePath, bin); } MessageBox.Show("Xuất excel thành công!"); } catch (Exception EE) { MessageBox.Show(EE.Message); } }