CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToExcelClient.cs
2024-08-08 16:12:26 +04:00

117 lines
3.9 KiB
C#

using DocumentFormat.OpenXml.Packaging;
using ElectronicsShopBusinessLogic.OfficePackage.HelperEnums;
using ElectronicsShopBusinessLogic.OfficePackage.HelperModels;
namespace ElectronicsShopBusinessLogic.OfficePackage
{
public abstract class AbstractSaveToExcelClient
{
public byte[]? CreateReport(ExcelInfoClient info) {
CreateExcel(info);
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "A",
RowIndex = 1,
Text = info.Title,
StyleInfo = ExcelStyleInfoType.Title
});
MergeCells(new ExcelMergeParameters {
CellFromName = "A1",
CellToName = "C1"
});
uint rowIndex = 2;
foreach (var pp in info.PaymeantProducts) {
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "A",
RowIndex = rowIndex,
Text = $"Номер оплаты: {pp.PaymeantID}",
StyleInfo = ExcelStyleInfoType.Text
});
MergeCells(new ExcelMergeParameters {
CellFromName = $"A{rowIndex}",
CellToName = $"B{rowIndex}"
});
rowIndex++;
foreach (var (Product, Count, CostItem, Price) in pp.Products) {
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "B",
RowIndex = rowIndex,
Text = Product,
StyleInfo = ExcelStyleInfoType.TextWithBroder
});
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "C",
RowIndex = rowIndex,
StyleInfo = ExcelStyleInfoType.TextWithBroder
});
MergeCells(new ExcelMergeParameters {
CellFromName = $"B{rowIndex}",
CellToName = $"C{rowIndex}"
});
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "D",
RowIndex = rowIndex,
Text = Count.ToString(),
StyleInfo = ExcelStyleInfoType.TextWithBroder
});
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "E",
RowIndex = rowIndex,
Text = CostItem,
StyleInfo = ExcelStyleInfoType.TextWithBroder,
});
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "F",
RowIndex = rowIndex,
Text = Price.ToString(),
StyleInfo = ExcelStyleInfoType.TextWithBroder,
});
rowIndex++;
}
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "A",
RowIndex = rowIndex,
Text = "Итого:",
StyleInfo = ExcelStyleInfoType.Title
});
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "C",
RowIndex = rowIndex,
Text = pp.TotalCount.ToString(),
StyleInfo = ExcelStyleInfoType.Title
});
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "D",
RowIndex = rowIndex,
Text = pp.PaymeantPrice.ToString(),
StyleInfo = ExcelStyleInfoType.Title
});
rowIndex++;
}
var documnet = SaveExcel(info);
return documnet;
}
// Создание excel-файла
protected abstract void CreateExcel(ExcelInfoClient info);
// Добавляем новую ячейку в лист
protected abstract void InsertCellInWorksheet(ExcelCellParameters excelParams);
// Объединение ячеек
protected abstract void MergeCells(ExcelMergeParameters excelParams);
// Сохранение файла
protected abstract byte[]? SaveExcel(ExcelInfoClient info);
}
}