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 users, List 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 + missed) == 0 ? 0 : (attended * 100.0) / (attended + missed); worksheet.Cell(row, 1).Value = user.FIO; worksheet.Cell(row, 2).SetValue(attended).Style.NumberFormat.Format = "0"; // Устанавливаем формат числа worksheet.Cell(row, 3).SetValue(missed).Style.NumberFormat.Format = "0"; // Устанавливаем формат числа worksheet.Cell(row, 4).SetValue(percentage).Style.NumberFormat.Format = "0.00"; // Устанавливаем формат числа 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 usersForExport, List 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 + missed) == 0 ? 0 : (attended * 100.0) / (attended + missed); worksheet.Cell(row, 1).Value = user.FIO; worksheet.Cell(row, 2).SetValue(attended).Style.NumberFormat.Format = "0"; // Устанавливаем формат числа worksheet.Cell(row, 3).SetValue(missed).Style.NumberFormat.Format = "0"; // Устанавливаем формат числа worksheet.Cell(row, 4).SetValue(percentage).Style.NumberFormat.Format = "0.00"; // Устанавливаем формат числа if (percentage < 40) { worksheet.Row(row).Style.Fill.BackgroundColor = XLColor.Red; } row++; } workbook.SaveAs(filePath); } Console.WriteLine($"Отчёт экспортирован в Excel: {filePath}"); } } }