using CompanyManagement.Infrastructure.Excel.EmployeeBankInfo; using OfficeOpenXml.Style; using OfficeOpenXml; using System.Drawing; using _0_Framework.Application; namespace CompanyManagement.Infrastructure.Excel.CWS; public class CustomizeWorkshopGroupExcelViewModel { public string Name { get; set; } public string ShiftType { get; set; } public string Shifts { get; set; } public string Salary { get; set; } public List EmployeeSettings { get; set; } } public class CustomizeWorkshopEmployeeExcelViewModel { public string Name { get; set; } public string ShiftType { get; set; } public string Shifts { get; set; } public int LeavePermitted { get; set; } public string Salary { get; set; } } public class CustomizeWorkshopGroupSettingExcelGenerator { public static byte[] Generate(List groups) { ExcelPackage.LicenseContext = LicenseContext.NonCommercial; using (var package = new ExcelPackage()) { var worksheet = package.Workbook.Worksheets.Add("GroupsAndEmployees"); // Headers worksheet.Cells[1, 1].Value = "نام گروه"; worksheet.Cells[1, 2].Value = "نوع شیفت گروه"; worksheet.Cells[1, 3].Value = "شیفت گروه"; worksheet.Cells[1, 4].Value = "حقوق گروه"; worksheet.Cells[1, 5].Value = "نام پرسنل"; worksheet.Cells[1, 6].Value = "مجاز مرخصی پرسنل"; worksheet.Cells[1, 7].Value = "نوع شیفت پرسنل"; worksheet.Cells[1, 8].Value = "شیفت پرسنل"; worksheet.Cells[1, 9].Value = "حقوق پرسنل"; using (var range = worksheet.Cells[1, 1, 1, 9]) { range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; range.Style.VerticalAlignment = ExcelVerticalAlignment.Center; range.Style.Font.Bold = true; range.Style.Fill.PatternType = ExcelFillStyle.Solid; range.Style.Fill.BackgroundColor.SetColor(Color.LightGray); // رنگ پس زمینه خاکستری range.Style.Border.BorderAround(ExcelBorderStyle.Thin); } int row = 2; foreach (var group in groups) { var employees = group.EmployeeSettings ?? new List(); int groupStartRow = row; int groupEndRow = employees.Count > 0 ? (row + employees.Count - 1) : row; if (employees.Count == 0) { // گروه بدون پرسنل worksheet.Cells[row, 1].Value = group.Name; worksheet.Cells[row, 2].Value = group.ShiftType; worksheet.Cells[row, 3].Value = group.Shifts; worksheet.Cells[row, 4].Value = Convert.ToInt32(group.Salary); ApplyGroupStyle(worksheet, row, row); worksheet.Cells[row, 4].Style.Numberformat.Format = "#,##0"; using (var thickBorderRange = worksheet.Cells[groupEndRow, 1, groupEndRow, 9]) { thickBorderRange.Style.Border.Bottom.Style = ExcelBorderStyle.Thick; } row++; } else { foreach (var employee in employees) { worksheet.Cells[row, 5].Value = employee.Name; worksheet.Cells[row, 6].Value = employee.LeavePermitted; worksheet.Cells[row, 7].Value = employee.ShiftType; worksheet.Cells[row, 8].Value = employee.Shifts; worksheet.Cells[row, 9].Value = Convert.ToInt32(employee.Salary); ApplyEmployeeStyle(worksheet, row); worksheet.Cells[row, 9].Style.Numberformat.Format = "#,##0"; row++; } // Merge اطلاعات گروه برای تمام پرسنل worksheet.Cells[groupStartRow, 1, groupEndRow, 1].Merge = true; worksheet.Cells[groupStartRow, 1].Value = group.Name; worksheet.Cells[groupStartRow, 2, groupEndRow, 2].Merge = true; worksheet.Cells[groupStartRow, 2].Value = group.ShiftType; worksheet.Cells[groupStartRow, 3, groupEndRow, 3].Merge = true; worksheet.Cells[groupStartRow, 3].Value = group.Shifts; worksheet.Cells[groupStartRow, 4, groupEndRow, 4].Merge = true; worksheet.Cells[groupStartRow, 4].Value = Convert.ToInt32(group.Salary); ApplyGroupStyle(worksheet, groupStartRow, groupEndRow); worksheet.Cells[groupStartRow, 4, groupEndRow, 4].Style.Numberformat.Format = "#,##0"; worksheet.Cells[groupEndRow, 1, groupEndRow, 9].Style.Border.Bottom.Style = ExcelBorderStyle.Thick; } } worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns(); for (int i = 2; i <= worksheet.Dimension.Rows; i++) { worksheet.Row(i).Height = 50; } int groupShiftCol = 3; int employeeShiftCol = 8; int groupSalary = 4; int employeeSalary = 9; int employeeName =5; worksheet.Columns[groupShiftCol].Width = 15; worksheet.Columns[employeeShiftCol].Width = 15; worksheet.Columns[groupSalary].Width = 16; worksheet.Columns[employeeSalary].Width = 16; worksheet.Columns[employeeName].Width = 24; worksheet.View.RightToLeft = true; // فارسی return package.GetAsByteArray(); } } private static void ApplyEmployeeStyle(ExcelWorksheet worksheet, int row) { using (var range = worksheet.Cells[row, 5, row, 9]) { //range.Style.Fill.PatternType = ExcelFillStyle.Solid; //range.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(198, 239, 206)); // سبز خیلی روشن range.Style.Border.BorderAround(ExcelBorderStyle.Thin); range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; range.Style.VerticalAlignment = ExcelVerticalAlignment.Center; // اضافه کردن بوردر به همه‌ی سلول‌های ردیف پرسنل range.Style.Border.Top.Style = ExcelBorderStyle.Thin; range.Style.Border.Bottom.Style = ExcelBorderStyle.Thin; range.Style.Border.Left.Style = ExcelBorderStyle.Thin; range.Style.Border.Right.Style = ExcelBorderStyle.Thin; range.Style.WrapText = true; } } private static void ApplyGroupStyle(ExcelWorksheet worksheet, int startRow, int endRow) { using (var range = worksheet.Cells[startRow, 1, endRow, 4]) { //range.Style.Fill.PatternType = ExcelFillStyle.Solid; //range.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(255, 255, 204)); // زرد خیلی روشن range.Style.Border.BorderAround(ExcelBorderStyle.Thin); range.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; range.Style.VerticalAlignment = ExcelVerticalAlignment.Center; // اضافه کردن بوردر به همه‌ی سلول‌های گروه range.Style.Border.Top.Style = ExcelBorderStyle.Thin; range.Style.Border.Bottom.Style = ExcelBorderStyle.Thin; range.Style.Border.Left.Style = ExcelBorderStyle.Thin; range.Style.Border.Right.Style = ExcelBorderStyle.Thin; range.Style.WrapText = true; } } }