CourseWorkElectronicsShop/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToExcelClient.cs
Илья Федотов a9ec52a3f7 Excel order
2024-07-27 13:38:35 +04:00

101 lines
3.6 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.ToString(),
StyleInfo = ExcelStyleInfoType.Text
});
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,
Text = Count.ToString(),
StyleInfo = ExcelStyleInfoType.TextWithBroder
});
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "D",
RowIndex = rowIndex,
Text = CostItem,
StyleInfo = ExcelStyleInfoType.TextWithBroder,
});
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "E",
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);
}
}