slarny4/Demo1/UI/ExcelExporter.cs

99 lines
4.5 KiB
C#
Raw Normal View History

2024-11-25 04:33:26 +00:00
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);
2024-12-02 10:24:02 +00:00
// Проверка на ноль перед вычислением процента посещаемости
double percentage = (attended + missed) == 0 ? 0 : (attended * 100.0) / (attended + missed);
2024-11-25 04:33:26 +00:00
worksheet.Cell(row, 1).Value = user.FIO;
2024-12-02 10:24:02 +00:00
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"; // Устанавливаем формат числа
2024-11-25 04:33:26 +00: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<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);
2024-12-02 10:24:02 +00:00
// Проверка на ноль перед вычислением процента посещаемости
double percentage = (attended + missed) == 0 ? 0 : (attended * 100.0) / (attended + missed);
2024-11-25 04:33:26 +00:00
worksheet.Cell(row, 1).Value = user.FIO;
2024-12-02 10:24:02 +00:00
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"; // Устанавливаем формат числа
2024-11-25 04:33:26 +00:00
if (percentage < 40)
{
worksheet.Row(row).Style.Fill.BackgroundColor = XLColor.Red;
}
row++;
}
workbook.SaveAs(filePath);
}
Console.WriteLine($"Отчёт экспортирован в Excel: {filePath}");
}
}
}