add excel export for file page
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
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<string, string> Header { get; set; } = new()
|
||||
{
|
||||
{"ArchiveNumber","شماره بایگانی"},
|
||||
{"FileClass","کلاسه پرونده"},
|
||||
{"Client","موکل"},
|
||||
{"LitigationParty","طرف دعوی"},
|
||||
{"DiagnosisPetitionTotalPenalty","مبلغ دادنامه تشخیص"},
|
||||
{"DisputeResolutionTotalPenalty","مبلغ دادنامه حل اختلاف"},
|
||||
{"MclsUsername","نام کاربری"},
|
||||
{"MclsPassword","رمز عبور"},
|
||||
|
||||
};
|
||||
public static byte[] GenerateCheckoutTempExcelInfo(List<FileExcelViewModel> data)
|
||||
{
|
||||
OfficeOpenXml.ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
using var package = new ExcelPackage();
|
||||
var worksheet = package.Workbook.Worksheets.Add("Sheet1");
|
||||
|
||||
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;
|
||||
return package.GetAsByteArray();
|
||||
}
|
||||
|
||||
private static bool RequiresMoneyToDouble(string propertyName)
|
||||
{
|
||||
var propertiesRequiringConversion = new HashSet<string>
|
||||
{
|
||||
"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;
|
||||
}
|
||||
}
|
||||
@@ -13,5 +13,8 @@
|
||||
<ProjectReference Include="..\AccountMangement.Infrastructure.EFCore\AccountMangement.Infrastructure.EFCore.csproj" />
|
||||
<ProjectReference Include="..\CompanyManagment.EFCore\CompanyManagment.EFCore.csproj" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Folder Include="CaseManagement\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user