using System.Drawing; using OfficeOpenXml.Style; using OfficeOpenXml; using CompanyManagement.Infrastructure.Excel.Checkout; using CompanyManagment.App.Contracts.File1; using Microsoft.AspNetCore.Server.HttpSys; using _0_Framework.Application; namespace CompanyManagement.Infrastructure.Excel.CaseManagement; public class CaseManagementExcelGenerator { public static Dictionary Header { get; set; } = new() { {"ArchiveNumber","شماره بایگانی"}, {"FileClass","کلاسه پرونده"}, {"Client","موکل"}, {"LitigationParty","طرف دعوی"}, {"DiagnosisPetitionTotalPenalty","مبلغ دادنامه تشخیص"}, {"DisputeResolutionTotalPenalty","مبلغ دادنامه حل اختلاف"}, {"MclsUsername","نام کاربری"}, {"MclsPassword","رمز عبور"}, }; public static byte[] GenerateCheckoutTempExcelInfo(List data) { OfficeOpenXml.ExcelPackage.License.SetNonCommercialOrganization("Gozareshgir Noncommercial organization"); using var package = new ExcelPackage(); CreateSheet(data, package,"همه"); CreateSheet(data.Where(x=>x.Status ==2).ToList(), package,"فعال"); CreateSheet(data.Where(x=>x.Status == 1).ToList(), package,"غیرفعال"); CreateSheet(data.Where(x=>x.Status == 3).ToList(), package,"خروج موقت"); return package.GetAsByteArray(); } private static void CreateSheet(List data, ExcelPackage package,string sheetName) { var worksheet = package.Workbook.Worksheets.Add(sheetName); var indexCell = worksheet.Cells[1, 1]; indexCell.Value = "ردیف"; ApplyHeaderStyle(indexCell); for (int i = 0; i < Header.Count; i++) { worksheet.Cells[1, i + 2].Value = Header.ElementAt(i).Value; ApplyHeaderStyle(worksheet.Cells[1, i + 2]); } var dataRow = 2; foreach (var item in data) { var column = 2; foreach (var header in Header) { var property = item.GetType().GetProperty(header.Key); var value = property.GetValue(item, null)?.ToString(); var cell = worksheet.Cells[dataRow, column]; // Check if the property requires MoneyToDouble() if (RequiresMoneyToDouble(property.Name)) { cell.Value = MoneyToDouble(value); cell.Style.Numberformat.Format = "#,##0"; } else { cell.Value = value; } ApplyGeneralDataStyle(worksheet.Cells[dataRow, column]); switch (item.Status) { case 1: cell.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Gray); break; case 2: cell.Style.Fill.BackgroundColor.SetColor(Color.White); break; case 3: cell.Style.Fill.BackgroundColor.SetColor(Color.Orange); break; } column++; } var rowCounter = worksheet.Cells[dataRow, 1]; rowCounter.Value = dataRow - 1; dataRow++; } worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns(); worksheet.View.RightToLeft = true; } private static bool RequiresMoneyToDouble(string propertyName) { var propertiesRequiringConversion = new HashSet { "DiagnosisPetitionTotalPenalty", "DisputeResolutionTotalPenalty" }; return propertiesRequiringConversion.Contains(propertyName); } private static double MoneyToDouble(string value) { Console.WriteLine(value); var min = value.Length > 1 ? value.Substring(0, 2) : ""; var test = min == "\u200e\u2212" ? value.MoneyToDouble() * -1 : value.MoneyToDouble(); Console.WriteLine(test); return test; } private static void ApplyHeaderStyle(ExcelRange cell) { cell.Style.Font.Bold = true; cell.Style.Fill.PatternType = ExcelFillStyle.Solid; cell.Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightGray); cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; cell.Style.Border.BorderAround(ExcelBorderStyle.Thin); } private static void ApplyGeneralDataStyle(ExcelRange cell) { cell.Style.Border.BorderAround(ExcelBorderStyle.Thin); cell.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; cell.Style.VerticalAlignment = ExcelVerticalAlignment.Center; cell.Style.Fill.PatternType = ExcelFillStyle.Solid; } }