using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using UniversityBusinessLogics.OfficePackage.HelperEnums; using UniversityBusinessLogics.OfficePackage.HelperModels.Excel; using UniversityContracts.ViewModels; namespace UniversityBusinessLogics.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 }); } } /// /// Создание отчета /// /// public void CreateReportPurchase(ExcelInfo info) { CreateExcel(info); CreateHeaders(info); uint rowIndex = 3; foreach (var pc in info.ReportObjects) { if (pc is not PurchaseViewModel purchase) { throw new ArgumentException($"Передан некорректный тип в отчет: " + $"ожидается PurchaseViewModel; Получен объект типа: {pc.GetType()}", nameof(info)); } InsertCellInWorksheet(new ExcelCellParameters { ColumnName = "B", RowIndex = rowIndex, Text = purchase.DatePurchase.ToShortDateString(), StyleInfo = ExcelStyleInfoType.Text }); InsertCellInWorksheet(new ExcelCellParameters { ColumnName = "A", RowIndex = rowIndex, Text = purchase.Id.ToString(), StyleInfo = ExcelStyleInfoType.Text }); int i = 0; foreach (var car in purchase.ClassViewModels) { if (info.Ids != null && !info.Ids.Contains(car.Id)) { continue; } InsertCellInWorksheet(new ExcelCellParameters { ColumnName = ((char)('C' + i)).ToString(), RowIndex = rowIndex, Text = $"{car.Time} {car.Name} в количестве {purchase.ClassModel[car.Id].CountClass}", StyleInfo = ExcelStyleInfoType.TextWithBroder }); if (info.Ids != null) { break; } i++; } rowIndex++; } SaveExcel(info); } public void CreateReportOperations(ExcelInfo info) { CreateExcel(info); CreateHeaders(info); uint rowIndex = 3; foreach (var pc in info.ReportObjects) { var car = pc as ClassViewModel; if (car == null) { throw new ArgumentException($"Передан некорректный тип в отчет: " + $"ожидается OperationViewModel; Получен объект типа: {pc.GetType()}", nameof(info)); } foreach (var purchase in car.Purchases) { if (info.Ids != null && !info.Ids.Contains(purchase.Id)) { continue; } InsertCellInWorksheet(new ExcelCellParameters { ColumnName = "A", RowIndex = rowIndex, Text = car.Time, StyleInfo = ExcelStyleInfoType.Text }); InsertCellInWorksheet(new ExcelCellParameters { ColumnName = "B", RowIndex = rowIndex, Text = car.Name, StyleInfo = ExcelStyleInfoType.Text }); InsertCellInWorksheet(new ExcelCellParameters { ColumnName = "C", RowIndex = rowIndex, Text = purchase.ClassModel[car.Id].CountClass.ToString(), StyleInfo = ExcelStyleInfoType.Text }); InsertCellInWorksheet(new ExcelCellParameters { ColumnName = "C", RowIndex = rowIndex, Text = purchase.DatePurchase.ToString(), StyleInfo = ExcelStyleInfoType.Text }); break; } rowIndex++; } SaveExcel(info); } /// /// Создание excel-файла /// /// protected abstract void CreateExcel(ExcelInfo info); /// /// Добавляем новую ячейку в лист /// /// protected abstract void InsertCellInWorksheet(ExcelCellParameters excelParams); /// /// Объединение ячеек /// /// protected abstract void MergeCells(ExcelMergeParameters excelParams); /// /// Сохранение файла /// /// protected abstract void SaveExcel(ExcelInfo info); } }