ISEbd-22_CourseWork_School/School/SchoolBusinessLogics/OfficePackage/AbstractSaveToExcel.cs

165 lines
6.2 KiB
C#

using SchoolBusinessLogics.OfficePackage.HelperEnums;
using SchoolBusinessLogics.OfficePackage.HelperModels;
using SchoolContracts.ViewModels;
namespace SchoolBusinessLogics.OfficePackage
{
public abstract class AbstractSaveToExcel
{
private void CreateHeaders(ExcelInfo info)
{
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "A",
RowIndex = 1,
Text = info.Title,
StyleInfo = ExcelStyleInfoType.Title
});
MergeCells(new ExcelMergeParameters
{
CellFromName = "A1",
CellToName = "G1"
});
for (var i = 0; i < info.Headers.Count; i++)
{
InsertCellInWorksheet(new ExcelCellParameters()
{
ColumnName = ((char)('A' + i)).ToString(),
RowIndex = 2,
Text = info.Headers[i],
StyleInfo = ExcelStyleInfoType.Text
});
}
}
/// <summary>
/// Создание отчета
/// </summary>
/// <param name="info"></param>
public void CreateReportDisciplines(ExcelInfo info)
{
CreateExcel(info);
CreateHeaders(info);
uint rowIndex = 3;
foreach (var pc in info.ReportObjects)
{
var discipline = pc as DisciplineViewModel;
if (discipline == null)
{
throw new ArgumentException($"Передан некорректный тип в отчет: " +
$"ожидается DisciplineViewModel; Получен объект типа: {pc.GetType()}", nameof(info));
}
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "A",
RowIndex = rowIndex,
Text = discipline.Name,
StyleInfo = ExcelStyleInfoType.Text
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "B",
RowIndex = rowIndex,
Text = discipline.DateOfReceipt.ToShortDateString(),
StyleInfo = ExcelStyleInfoType.Text
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "C",
RowIndex = rowIndex,
Text = discipline.DateOfPassage.ToShortDateString(),
StyleInfo = ExcelStyleInfoType.Text
});
for (var i = 0; i < discipline.ClientViewModels.Count; i++)
{
var client = discipline.ClientViewModels[i];
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = ((char)('D' + i)).ToString(),
RowIndex = rowIndex,
Text = $"{client.Name} на {discipline.ClientsModel[client.Id].DateOfClient.ToShortDateString()}",
StyleInfo = ExcelStyleInfoType.TextWithBroder
});
}
rowIndex++;
}
SaveExcel(info);
}
public void CreateReportClients(ExcelInfo info)
{
CreateExcel(info);
CreateHeaders(info);
uint rowIndex = 3;
foreach (var pc in info.ReportObjects)
{
var student = pc as StudentViewModel;
if (student == null)
{
throw new ArgumentException($"Передан некорректный тип в отчет: " +
$"ожидается StudentViewModel; Получен объект типа: {pc.GetType()}", nameof(info));
}
foreach (var discipline in student.Disciplines)
{
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "A",
RowIndex = rowIndex,
Text = student.Name,
StyleInfo = ExcelStyleInfoType.Text
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "B",
RowIndex = rowIndex,
Text = discipline.StudentsModel[student.Id].DateOfStudent.ToShortDateString(),
StyleInfo = ExcelStyleInfoType.Text
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "C",
RowIndex = rowIndex,
Text = discipline.Name,
StyleInfo = ExcelStyleInfoType.Text
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "D",
RowIndex = rowIndex,
Text = student.Course.ToString(),
StyleInfo = ExcelStyleInfoType.Text
});
}
rowIndex++;
}
SaveExcel(info);
}
/// <summary>
/// Создание excel-файла
/// </summary>
/// <param name="info"></param>
protected abstract void CreateExcel(ExcelInfo info);
/// <summary>
/// Добавляем новую ячейку в лист
/// </summary>
protected abstract void InsertCellInWorksheet(ExcelCellParameters excelParams);
/// <summary>
/// Объединение ячеек
/// </summary>
protected abstract void MergeCells(ExcelMergeParameters excelParams);
/// <summary>
/// Сохранение файла
/// </summary>
/// <param name="info"></param>
protected abstract void SaveExcel(ExcelInfo info);
}
}