PIbd-21_Danilov_V.V._Course.../VeterinaryClinic/VeterinaryClinicBusinessLogics/OfficePackage/AbstractSaveToExcel.cs

194 lines
5.6 KiB
C#
Raw Normal View History

using DocumentFormat.OpenXml.Presentation;
using VeterinaryClinicBusinessLogics.OfficePackage.HelperEnums;
using VeterinaryClinicBusinessLogics.OfficePackage.HelperModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VeterinaryClinicBusinessLogics.OfficePackage
{
/// <summary>
/// Абстрактный класс для создания отчета Excel
/// </summary>
public abstract class AbstractSaveToExcel
{
/// <summary>
/// Создать отчет Excel
/// </summary>
/// <param name="info"></param>
public void CreateReport(ExcelInfo info)
{
// Создаем файл
CreateExcel(info);
// Создаем заголовок таблицы
// "Список животных."
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "A",
RowIndex = 1,
Text = info.Title,
StyleInfo = ExcelStyleInfoType.Title
});
// Объединяем ячейки A1:H1 для заголовка таблицы
MergeCells(new ExcelMergeParameters
{
CellFromName = "A1",
CellToName = "H1"
});
// Записываем основную информацию
uint rowIndex = 2;
foreach (var view in info.AnimalServices)
{
// "Животное:"
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "A",
RowIndex = rowIndex,
Text = "Животное:",
StyleInfo = ExcelStyleInfoType.SubtitleWithBorder
});
// Номер животного "№X"
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "B",
RowIndex = rowIndex,
Text = $"№{view.Animal.Id}",
StyleInfo = ExcelStyleInfoType.TextWithBorder
});
// "Вид:"
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "C",
RowIndex = rowIndex,
Text = "Вид:",
StyleInfo = ExcelStyleInfoType.SubtitleWithBorder
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "D",
RowIndex = rowIndex,
Text = $"{view.Animal.Type}",
StyleInfo = ExcelStyleInfoType.TextWithBorder
});
// "Порода:"
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "E",
RowIndex = rowIndex,
Text = "Порода:",
StyleInfo = ExcelStyleInfoType.SubtitleWithBorder
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "F",
RowIndex = rowIndex,
Text = $"{view.Animal.Breed}",
StyleInfo = ExcelStyleInfoType.TextWithBorder
});
// "Возраст:"
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "G",
RowIndex = rowIndex,
Text = "Возраст:",
StyleInfo = ExcelStyleInfoType.SubtitleWithBorder
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "H",
RowIndex = rowIndex,
Text = $"{view.Animal.Age}",
StyleInfo = ExcelStyleInfoType.TextWithBorder
});
rowIndex++;
// "Услуги:"
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "A",
RowIndex = rowIndex,
Text = "Услуги:",
StyleInfo = ExcelStyleInfoType.Subtitle
});
MergeCells(new ExcelMergeParameters
{
CellFromName = "A" + rowIndex,
CellToName = "B" + rowIndex
});
// Список услуг
foreach (var service in view.Services)
{
// "Название услуги"
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "C",
RowIndex = rowIndex,
Text = $"{service.Name}",
StyleInfo = ExcelStyleInfoType.Text
});
MergeCells(new ExcelMergeParameters
{
CellFromName = "C" + rowIndex,
CellToName = "E" + rowIndex
});
// "Стоимость услуги"
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "F",
RowIndex = rowIndex,
Text = $"{service.Cost}",
StyleInfo = ExcelStyleInfoType.Text
});
MergeCells(new ExcelMergeParameters
{
CellFromName = "F" + rowIndex,
CellToName = "H" + rowIndex
});
rowIndex++;
}
}
SaveExcel(info);
}
/// <summary>
/// Создать файл Excel
/// </summary>
/// <param name="info"></param>
protected abstract void CreateExcel(ExcelInfo info);
/// <summary>
/// Добавить новую ячейку в лист
/// </summary>
/// <param name="excelParams"></param>
protected abstract void InsertCellInWorksheet(ExcelCellParameters excelParams);
/// <summary>
/// Объединить ячейки
/// </summary>
/// <param name="excelParams"></param>
protected abstract void MergeCells(ExcelMergeParameters excelParams);
/// <summary>
/// Сохранить файл Excel
/// </summary>
/// <param name="info"></param>
protected abstract void SaveExcel(ExcelInfo info);
}
}