slarny4/Demo1/UI/ExcelExporter.cs

95 lines
3.7 KiB
C#

using ClosedXML.Excel;
using Demo.Domain.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Demo.UI
{
public class ExcelExporter
{
public void ExportGroupInfoToExcel(Group group, List<User> users, List<Presence> presence, string filePath)
{
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add("Group Info");
worksheet.Cell(1, 1).Value = "Group Name";
worksheet.Cell(1, 2).Value = group.Name;
worksheet.Cell(3, 1).Value = "Student Name";
worksheet.Cell(3, 2).Value = "Attended Lessons";
worksheet.Cell(3, 3).Value = "Missed Lessons";
worksheet.Cell(3, 4).Value = "Attendance Percentage";
int row = 4;
foreach (var user in users)
{
var userPresence = presence.Where(p => p.UserId == user.Id).ToList();
int attended = userPresence.Count(p => p.IsAttendance);
int missed = userPresence.Count(p => !p.IsAttendance);
double percentage = (attended * 100.0) / (attended + missed);
worksheet.Cell(row, 1).Value = user.FIO;
worksheet.Cell(row, 2).Value = attended;
worksheet.Cell(row, 3).Value = missed;
worksheet.Cell(row, 4).Value = percentage;
if (percentage < 40)
{
worksheet.Row(row).Style.Fill.BackgroundColor = XLColor.Red;
}
row++;
}
workbook.SaveAs(filePath);
}
Console.WriteLine($"Отчёт экспортирован в Excel: {filePath}");
}
// Реализация метода для DAO моделей
internal void ExportGroupInfoToExcel(Data.RemoteData.RemoteDataBase.DAO.Group groupForExport, List<Data.RemoteData.RemoteDataBase.DAO.User> usersForExport, List<Presence> presenceForExport, string filePath)
{
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add("Group Info DAO");
worksheet.Cell(1, 1).Value = "Group Name";
worksheet.Cell(1, 2).Value = groupForExport.Name;
worksheet.Cell(3, 1).Value = "Student Name";
worksheet.Cell(3, 2).Value = "Attended Lessons";
worksheet.Cell(3, 3).Value = "Missed Lessons";
worksheet.Cell(3, 4).Value = "Attendance Percentage";
int row = 4;
foreach (var user in usersForExport)
{
var userPresence = presenceForExport.Where(p => p.UserId == user.Id).ToList();
int attended = userPresence.Count(p => p.IsAttendance);
int missed = userPresence.Count(p => !p.IsAttendance);
double percentage = (attended * 100.0) / (attended + missed);
worksheet.Cell(row, 1).Value = user.FIO;
worksheet.Cell(row, 2).Value = attended;
worksheet.Cell(row, 3).Value = missed;
worksheet.Cell(row, 4).Value = percentage;
if (percentage < 40)
{
worksheet.Row(row).Style.Fill.BackgroundColor = XLColor.Red;
}
row++;
}
workbook.SaveAs(filePath);
}
Console.WriteLine($"Отчёт экспортирован в Excel: {filePath}");
}
}
}