add Excel export for workshop roll call data with new exporter and view model

This commit is contained in:
2025-11-23 12:52:07 +03:30
parent e6640a1636
commit 7e9ba23b97
4 changed files with 77 additions and 2 deletions

View File

@@ -0,0 +1,47 @@
using System.Drawing;
using CompanyManagment.App.Contracts.Workshop;
using OfficeOpenXml;
using OfficeOpenXml.Style;
namespace CompanyManagement.Infrastructure.Excel.WorkshopsRollCall;
public class WorkshopRollCallExcelExporter
{
public static byte[] Export(List<WorkshopRollCallExcelViewModel> workshops)
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using (var package = new ExcelPackage())
{
var ws = package.Workbook.Worksheets.Add("Workshops");
// Header
ws.Cells[1, 1].Value = "نام کارگاه";
ws.Cells[1, 2].Value = "فعال/غیرفعال";
ws.Cells[1, 3].Value = "تعداد پرسنل";
ws.Cells[1, 4].Value = "نام کارفرما";
ws.Row(1).Style.Font.Bold = true;
ws.Row(1).Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Row(1).Style.Fill.BackgroundColor.SetColor(Color.LightGray);
int row = 2;
foreach (var w in workshops)
{
ws.Cells[row, 1].Value = w.WorkshopName;
ws.Cells[row, 2].Value = w.IsActive ? "فعال" : "غیرفعال";
ws.Cells[row, 3].Value = w.PersonnelCount;
ws.Cells[row, 4].Value = w.EmployerName;
if (!w.IsActive)
{
using (var range = ws.Cells[row, 1, row, 4])
{
range.Style.Fill.PatternType = ExcelFillStyle.Solid;
range.Style.Fill.BackgroundColor.SetColor(Color.LightGray);
}
}
row++;
}
ws.Cells[ws.Dimension.Address].AutoFitColumns();
return package.GetAsByteArray();
}
}
}

View File

@@ -0,0 +1,11 @@
namespace CompanyManagement.Infrastructure.Excel.WorkshopsRollCall
{
public class WorkshopRollCallExcelViewModel
{
public string WorkshopName { get; set; }
public bool IsActive { get; set; }
public int PersonnelCount { get; set; }
public string EmployerName { get; set; }
}
}

View File

@@ -28,6 +28,7 @@ using Parbad;
using Parbad.AspNetCore;
using Parbad.Gateway.Sepehr;
using System.ComponentModel.DataAnnotations;
using CompanyManagement.Infrastructure.Excel.WorkshopsRollCall;
using static ServiceHost.Areas.AdminNew.Pages.Company.AndroidApk.IndexModel2;
namespace ServiceHost.Areas.AdminNew.Pages.Company.AndroidApk
@@ -162,7 +163,23 @@ namespace ServiceHost.Areas.AdminNew.Pages.Company.AndroidApk
//await ChangeFridayWorkToWeeklyDayOfWeek();
//await SetPublicId();
await CreateDadmehrWorkshopFaceEmbedding();
var now = DateTime.Now;
var data = _context.RollCallServices
.Include(x => x.Workshop)
.ThenInclude(x => x.LeftWorks)
.Include(x => x.Workshop)
.ThenInclude(x => x.WorkshopEmployers)
.ThenInclude(x=>x.Employer)
.Select(x => new WorkshopRollCallExcelViewModel()
{
EmployerName = x.Workshop.WorkshopEmployers.First().Employer.FullName,
IsActive = x.IsActiveString == "true",
PersonnelCount = x.Workshop.LeftWorks.Count(l => l.StartWorkDate <= now && l.LeftWorkDate >= now),
WorkshopName = x.Workshop.WorkshopFullName
}).OrderByDescending(x=>x.IsActive).ToList();
var dataBytes = WorkshopRollCallExcelExporter.Export(data);
return File(dataBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"RollCallServices.xlsx");
ViewData["message"] = "تومام دو";
return Page();
}

View File

@@ -12,7 +12,7 @@
//"MesbahDb": "Data Source=DESKTOP-NUE119G\\MSNEW;Initial Catalog=Mesbah_db;Integrated Security=True"
//server
//"MesbahDbServer": "Data Source=171.22.24.15;Initial Catalog=mesbah_db;Persist Security Info=False;User ID=ir_db;Password=R2rNp[170]18[3019]#@ATt;TrustServerCertificate=true;",
"MesbahDbServer": "Data Source=171.22.24.15;Initial Catalog=mesbah_db;Persist Security Info=False;User ID=ir_db;Password=R2rNp[170]18[3019]#@ATt;TrustServerCertificate=true;",
//local
"MesbahDb": "Data Source=.;Initial Catalog=mesbah_db;Integrated Security=True;TrustServerCertificate=true;",