Files
Backend-Api/0_Framework/Excel/Checkout/CustomizeCheckoutExcelGenerator.cs
2025-01-06 23:14:03 +03:30

180 lines
8.3 KiB
C#

using System.Collections.Generic;
using System.Drawing;
using System.IO;
using _0_Framework.Application;
using OfficeOpenXml;
namespace _0_Framework.Excel.Checkout;
public class CustomizeCheckoutExcelGenerator
{
public static byte[] GenerateCheckoutTempExcelInfo(List<CustomizeCheckoutTempExcelViewModel> data)
{
OfficeOpenXml.ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using var package = new OfficeOpenXml.ExcelPackage();
var worksheet = package.Workbook.Worksheets.Add("Sheet1");
//worksheet.Cells["A1:AC1"].Merge = true;
//var headerImagePath = Path.Combine("wwwroot", "Excel", "header", "2.png");
//using (var stream = new FileStream(headerImagePath, FileMode.Open, FileAccess.Read))
//{
// var picture = worksheet.Drawings.AddPicture("HeaderImage", stream);
// picture.SetPosition(0, 0, 0, 0); // Set position to the merged cell
// //picture.SetSize(600, 75); // Adjust the size as needed
//}
string[] headers =
[
//جزییات
"ردیف", "سال", "ماه", "نام و نام خانوادگی", "شماره پرسنلی", "کدملی", "روز کارکرد",
//مطالبات
"حقوق ماهانه", "پاداش", "جمعه کاری", "اضافه کاری", "نوبت کاری", "شب کاری",
"حق تاهل", "حق فرزند", "عیدی", "سنوات", "مزد مرخصی",
//کسورات
"غیبت", "تاخیر در ورود", "تعجیل در خروج", "مساعده", "قسط وام", "جریمه", "حق بیمه", "مالیات",
//مجموع
"جمع مطالبات", "جمع کسورات", "مبلغ قابل پرداخت"
];
for (int i = 0; i < headers.Length; i++)
{
worksheet.Cells[1, i + 1].Value = headers[i];
}
// Lock cells A1 to H6
//worksheet.Cells.Style.Locked = false;
//worksheet.Cells["A1:AC1"].Style.Locked = true;
//// Set worksheet protection with password
//worksheet.Protection.IsProtected = true;
//worksheet.Protection.SetPassword("Gozareshgir2049"); // Set your desired password here
//worksheet.Protection.AllowSelectLockedCells = true; // Unlock all other cells
using (var range = worksheet.Cells[1, 1, 1, 29])
{
range.Style.Font.Bold = true;
range.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
range.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightGray);
range.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
range.Style.Border.Right.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Left.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Bottom.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
range.Style.Border.Top.Style = OfficeOpenXml.Style.ExcelBorderStyle.Thin;
}
var dataRow = 2;
var numberFormat = "#,##0";
for (int i = 0; i < data.Count; i++)
{
var checkout = data[i];
worksheet.Cells[i + dataRow, 1].Value = i + 1;
worksheet.Cells[i + dataRow, 2].Value = checkout.Year;
worksheet.Cells[i + dataRow, 3].Value = checkout.Month;
worksheet.Cells[i + dataRow, 4].Value = checkout.EmployeeFullName;
worksheet.Cells[i + dataRow, 5].Value = checkout.PersonnelCodeString;
worksheet.Cells[i + dataRow, 6].Value = checkout.NationalCode;
worksheet.Cells[i + dataRow, 7].Value = checkout.SumOfWorkingDays;
worksheet.Cells[i + dataRow, 8].Value = checkout.MonthlySalary.MoneyToDouble();
worksheet.Cells[i + dataRow, 9].Value = checkout.RewardPay.MoneyToDouble();
worksheet.Cells[i + dataRow, 10].Value = checkout.FridayPay.MoneyToDouble();
worksheet.Cells[i + dataRow, 11].Value = checkout.OvertimePay.MoneyToDouble();
worksheet.Cells[i + dataRow, 12].Value = checkout.ShiftPay.MoneyToDouble();
worksheet.Cells[i + dataRow, 13].Value = checkout.NightworkPay.MoneyToDouble();
worksheet.Cells[i + dataRow, 14].Value = checkout.MarriedAllowance.MoneyToDouble();
worksheet.Cells[i + dataRow, 15].Value = checkout.FamilyAllowance.MoneyToDouble();
worksheet.Cells[i + dataRow, 16].Value = checkout.BonusesPay.MoneyToDouble();
worksheet.Cells[i + dataRow, 17].Value = checkout.BaseYearsPay.MoneyToDouble();
worksheet.Cells[i + dataRow, 18].Value = checkout.LeavePay.MoneyToDouble();
worksheet.Cells[i + dataRow, 19].Value = checkout.AbsenceDeduction.MoneyToDouble();
worksheet.Cells[i + dataRow, 20].Value = checkout.LateToWorkDeduction.MoneyToDouble();
worksheet.Cells[i + dataRow, 21].Value = checkout.EarlyExitDeduction.MoneyToDouble();
worksheet.Cells[i + dataRow, 22].Value = checkout.SalaryAidDeduction.MoneyToDouble();
worksheet.Cells[i + dataRow, 23].Value = checkout.InstallmentDeduction.MoneyToDouble();
worksheet.Cells[i + dataRow, 24].Value = checkout.FineDeduction.MoneyToDouble();
worksheet.Cells[i + dataRow, 25].Value = checkout.InsuranceDeduction.MoneyToDouble();
worksheet.Cells[i + dataRow, 26].Value = checkout.TaxDeducation.MoneyToDouble();
worksheet.Cells[i + dataRow, 27].Value = checkout.TotalClaims.MoneyToDouble();
worksheet.Cells[i + dataRow, 28].Value = checkout.TotalDeductions.MoneyToDouble();
worksheet.Cells[i + dataRow, 29].Value = checkout.TotalPayment.MoneyToDouble();
// Style data cells
for (int j = 1; j <= 29; j++)
{
var cell = worksheet.Cells[i + 2, j];
cell.Style.Border.BorderAround(OfficeOpenXml.Style.ExcelBorderStyle.Thin);
cell.Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;
cell.Style.VerticalAlignment = OfficeOpenXml.Style.ExcelVerticalAlignment.Center;
cell.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
if (j >= 1 && j <= 7)
{
cell.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightYellow);
}
else if (j >= 8 && j <= 18)
{
cell.Style.Fill.BackgroundColor.SetColor(1,208,248,208);
cell.Style.Numberformat.Format = numberFormat;
}
else if (j >= 19 && j <= 26)
{
cell.Style.Fill.BackgroundColor.SetColor(1,246,176,176);
cell.Style.Numberformat.Format = numberFormat;
}
switch (j)
{
//جمع مطالبات
case 27:
cell.Style.Fill.BackgroundColor.SetColor(1, 169, 208, 142);
cell.Style.Numberformat.Format = numberFormat;
break;
//جمع کسورات
case 28:
cell.Style.Fill.BackgroundColor.SetColor(1, 241, 143, 143);
cell.Style.Numberformat.Format = numberFormat;
break;
//مبلغ قابل پرداخت
case 29:
cell.Style.Fill.BackgroundColor.SetColor(1, 168, 186, 254);
cell.Style.Numberformat.Format = numberFormat;
break;
}
}
}
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
worksheet.PrinterSettings.PaperSize = ePaperSize.A4;
worksheet.PrinterSettings.Orientation = eOrientation.Landscape;
worksheet.PrinterSettings.FitToPage = true;
worksheet.PrinterSettings.FitToWidth = 1;
worksheet.PrinterSettings.FitToHeight = 0;
worksheet.PrinterSettings.Scale = 85; // Scale to fit content within A4 width
worksheet.View.RightToLeft = true;
worksheet.PrinterSettings.RepeatRows = new OfficeOpenXml.ExcelAddress("1:1"); // Repeat rows 1 to 6 on every page
return package.GetAsByteArray();
}
}