Files
Backend-Api/CompanyManagement.Infrastructure.Excel/ContractingParty/InstitutionContractLegalExcelGenerator.cs
mahan 8faaea0a1e Add Excel export for institution contract and contracting party details
- Implement InstitutionContractDetailExcelGenerator and InstitutionContractLegalExcelGenerator for generating Excel files of contract parties (individual and legal)
- Add methods to Index.cshtml.cs to fetch and export contract party details within 6 months of contract end
- Update OnPostShiftDateNew to return contracting party Excel export
- Refactor usings to include new Excel generators
2025-11-30 15:30:43 +03:30

94 lines
3.8 KiB
C#

using System.Collections.Generic;
using OfficeOpenXml;
using OfficeOpenXml.Style;
using System.Drawing;
namespace CompanyManagement.Infrastructure.Excel.ContractingParty;
public static class InstitutionContractLegalExcelGenerator
{
public static byte[] Generate(List<InstitutionContractLegalExcelViewModel> items)
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using var package = new ExcelPackage();
var worksheet = package.Workbook.Worksheets.Add("جزئیات حقوقی طرف قرارداد");
// 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 = "شماره ملی";
// Header style
using (var headerRange = worksheet.Cells[1, 1, 1, 8])
{
headerRange.Style.Font.Bold = true;
headerRange.Style.Fill.PatternType = ExcelFillStyle.Solid;
headerRange.Style.Fill.BackgroundColor.SetColor(Color.LightGray);
headerRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
headerRange.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
headerRange.Style.Border.Top.Style = ExcelBorderStyle.Thin;
headerRange.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
headerRange.Style.Border.Left.Style = ExcelBorderStyle.Thin;
headerRange.Style.Border.Right.Style = ExcelBorderStyle.Thin;
}
// Data rows
int row = 2;
if (items != null)
{
foreach (var it in items)
{
worksheet.Cells[row, 1].Value = it.CompanyName;
worksheet.Cells[row, 2].Value = it.FirstName;
worksheet.Cells[row, 3].Value = it.LastName;
worksheet.Cells[row, 4].Value = it.NationalCode;
worksheet.Cells[row, 5].Value = it.RegistrationNumber;
worksheet.Cells[row, 6].Value = it.BirthDate;
worksheet.Cells[row, 7].Value = it.PhoneNumber;
worksheet.Cells[row, 8].Value = it.NationalNumber;
using (var dataRange = worksheet.Cells[row, 1, row, 8])
{
dataRange.Style.Border.Top.Style = ExcelBorderStyle.Thin;
dataRange.Style.Border.Bottom.Style = ExcelBorderStyle.Thin;
dataRange.Style.Border.Left.Style = ExcelBorderStyle.Thin;
dataRange.Style.Border.Right.Style = ExcelBorderStyle.Thin;
dataRange.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right; // راست‌چین
dataRange.Style.VerticalAlignment = ExcelVerticalAlignment.Center;
dataRange.Style.WrapText = true;
}
row++;
}
}
// Auto-fit columns to largest content
if (worksheet.Dimension != null)
{
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
}
// Set sheet to RTL for Persian
worksheet.View.RightToLeft = true;
return package.GetAsByteArray();
}
}
public class InstitutionContractLegalExcelViewModel
{
public string CompanyName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string NationalCode { get; set; }
public string RegistrationNumber { get; set; }
public string BirthDate { get; set; }
public string PhoneNumber { get; set; }
public string NationalNumber { get; set; }
}