add excel export for file page

This commit is contained in:
MahanCh
2025-04-14 16:02:51 +03:30
parent fa587c61eb
commit 4ffdf14de2
10 changed files with 247 additions and 6 deletions

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using _0_Framework_b.Domain;
using CompanyManagment.App.Contracts.Employee;
using CompanyManagment.App.Contracts.Employer;
@@ -22,4 +23,6 @@ public interface IFileRepository : IRepository<long, File1>
List<FileViewModel> GetFileList(FileSearchModel searchModel);
#endregion
Task<List<FileExcelViewModel>> GetExcelDetails();
}

View File

@@ -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;
}
}

View File

@@ -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>

View File

@@ -0,0 +1,49 @@
namespace CompanyManagment.App.Contracts.File1;
public class FileExcelViewModel
{
/// <summary>
/// شماره بایگانی
/// </summary>
public long ArchiveNumber { get; set; }
/// <summary>
/// کلاسه پرونده
/// </summary>
public string FileClass { get; set; } = string.Empty;
/// <summary>
/// موکل
/// </summary>
public string Client { get; set; } = string.Empty;
/// <summary>
/// طرف دعوی
/// </summary>
public string LitigationParty { get; set; } = string.Empty;
/// <summary>
/// مبلغ دادنامه تشخیص
/// </summary>
public string DiagnosisPetitionTotalPenalty { get; set; } = string.Empty;
/// <summary>
/// مبلغ دادنامه حل اختلاف
/// </summary>
public string DisputeResolutionTotalPenalty { get; set; } = string.Empty;
/// <summary>
/// نام کاربری اداره کار
/// </summary>
public string MclsUsername { get; set; } = string.Empty;
/// <summary>
/// رمز عبور اداره کار
/// </summary>
public string MclsPassword { get; set; } = string.Empty;
/// <summary>
/// وضعیت پرونده. یک به معنی غیرفعال. دو به معنی فعال. سه به معنی خروج موقت
/// </summary>
public int Status { get; set; }
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using _0_Framework_b.Application;
using CompanyManagment.App.Contracts.Employee;
using CompanyManagment.App.Contracts.Employer;
@@ -23,4 +24,5 @@ public interface IFileApplication
List<EmployeeViewModel> GetAllEmploees(bool filter = true);
List<EmployerViewModel> GetAllEmployers(bool filter = true);
//bool CheckValue(string viewModel, string searchModel);
Task<List<FileExcelViewModel>> GetExcelDetails();
}

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using _0_Framework_b.Application;
using Company.Domain.File1;
using Company.Domain.ProceedingSession;
@@ -199,6 +200,11 @@ public class FileApplication : IFileApplication
return employers;
}
public async Task<List<FileExcelViewModel>> GetExcelDetails()
{
return await _fileRepository.GetExcelDetails();
}
public FileViewModel GetFileDetails(FileViewModel file)
{
var viewModel = new FileViewModel();

View File

@@ -1,11 +1,13 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using _0_Framework_b.InfraStructure;
using _0_Framework_b.Application;
using Company.Domain.File1;
using CompanyManagment.App.Contracts.Employee;
using CompanyManagment.App.Contracts.Employer;
using CompanyManagment.App.Contracts.File1;
using Microsoft.EntityFrameworkCore;
namespace CompanyManagment.EFCore.Repository;
@@ -261,7 +263,7 @@ public class FileRepository : RepositoryBase<long, Company.Domain.File1.File1>,
EmployeeFullName = x.FName + " " + x.LName
}).FirstOrDefault();
return result?.EmployeeFullName??"";
return result?.EmployeeFullName ?? "";
}
public string GetEmployerFullNameById(long id)
@@ -271,7 +273,7 @@ public class FileRepository : RepositoryBase<long, Company.Domain.File1.File1>,
FullName = x.FullName
}).FirstOrDefault();
return result?.FullName??"";
return result?.FullName ?? "";
}
public List<EmployeeViewModel> GetAllEmploees()
@@ -361,4 +363,35 @@ public class FileRepository : RepositoryBase<long, Company.Domain.File1.File1>,
return query.OrderByDescending(x => x.ArchiveNo).Skip(searchModel.PageIndex).Take(30).ToList();
}
public async Task<List<FileExcelViewModel>> GetExcelDetails()
{
var fileQuery = _context.Files.Include(x => x.PetitionsList).AsQueryable();
var requesterEmployeeQuery = await _context.Employees.Where(x => fileQuery.Any(f => f.Reqester == x.id)).ToListAsync();
var summonedEmployerQuery = await _context.Employers.Where(x => fileQuery.Any(f => f.Summoned == x.id)).ToListAsync();
var files = await fileQuery.ToListAsync();
var res = files.Select(x =>
{
var requester = requesterEmployeeQuery.FirstOrDefault(e => e.id == x.Reqester);
var summoned = summonedEmployerQuery.FirstOrDefault(e => e.id == x.Summoned);
return new FileExcelViewModel
{
ArchiveNumber = x.ArchiveNo,
FileClass = x.FileClass,
Client = x.Client == 1 ? requester?.FullName : summoned?.FullName ?? "-",
LitigationParty = x.Client == 1 ? summoned?.FullName : requester?.FullName ?? "-",
DiagnosisPetitionTotalPenalty =
x.PetitionsList.FirstOrDefault(p => p.BoardType_Id == 1)?.TotalPenalty ?? "-",
DisputeResolutionTotalPenalty = x.PetitionsList.FirstOrDefault(p => p.BoardType_Id == 2)?.TotalPenalty ?? "-",
MclsUsername = x.Client == 1 ? requester?.MclsUserName : summoned?.MclsUserName ?? "-",
MclsPassword = x.Client == 1 ? requester?.MclsPassword : summoned?.MclsPassword ?? "-",
Status = x.Status
};
}).OrderBy(x=>x.Status ==1).ThenBy(x=>x.Status == 3).ThenBy(x=>x.Status==2);
return res.ToList();
}
}

View File

@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Mcls/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

View File

@@ -155,10 +155,14 @@
<p class="pull-right">
<a id="btnPopModal" href="#showmodal=@Url.Page("/Company/FilePage/Index", "CreateFile")" class="btn btn-success btn-rounded waves-effect waves-light m-b-5" style=" background-color: #f5f5f5; border-color: #0f9500; font-family: 'Web_Yekan' !important; color: #0f9500 !important; margin-right: 10px "> <i class="fa fa-user-plus" style="padding-left: 3px; font-size: 14px; color: #0f9500 !important "></i> ایجاد پرونده جدید </a>
</p>
<p class="pull-left">
<a id="btnPopModal" href="#showmodal=@Url.Page("/Company/FilePage/Index", "CreateOrEditFileTiming")" class="btn btn-success btn-rounded waves-effect waves-light m-b-5" style=" background-color: #f5f5f5; border-color: #448bdb; font-family: 'Web_Yekan' !important; color: #448bdb !important; margin-left: 10px "> <i class="fa fa-calendar" style="padding-right: 3px; font-size: 14px; color: #448bdb !important "></i> زمانبندی </a>
</p>
</div>
<p class="pull-left">
<a id="btnPopModal" href="#showmodal=@Url.Page("/Company/FilePage/Index", "CreateOrEditFileTiming")" class="btn btn-success btn-rounded waves-effect waves-light m-b-5" style=" background-color: #f5f5f5; border-color: #448bdb; font-family: 'Web_Yekan' !important; color: #448bdb !important; margin-left: 10px "> <i class="fa fa-calendar" style="padding-right: 3px; font-size: 14px; color: #448bdb !important "></i> زمانبندی </a>
</p>
<p class="pull-left">
<a id="ExcelDownload" href="@Url.Page("/Company/FilePage/Index", "ExcelFileDetails")" class="btn btn-success btn-rounded waves-effect waves-light m-b-5" style=" background-color: #f5f5f5; border-color: #448bdb; font-family: 'Web_Yekan' !important; color: #448bdb !important; margin-left: 10px "> <i class="fa fa-file" style="padding-right: 3px; font-size: 14px; color: #448bdb !important "></i> دانلود اکسل </a>
</p>
</div>
</div>

View File

@@ -1,4 +1,5 @@
using _0_Framework_b.Application;
using CompanyManagement.Infrastructure.Excel.CaseManagement;
using CompanyManagment.App.Contracts.Board;
using CompanyManagment.App.Contracts.Contact2;
using CompanyManagment.App.Contracts.Evidence;
@@ -650,4 +651,17 @@ public class IndexModel : PageModel
return new JsonResult(result);
}
#region Mahan
public async Task<IActionResult> OnGetExcelFileDetails()
{
var data = await _fileApplication.GetExcelDetails();
var excelBytes = CaseManagementExcelGenerator.GenerateCheckoutTempExcelInfo(data);
return File(excelBytes,
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
$"پرونده.xlsx");
}
#endregion
}