From c12b242073582d7c82e682c8ca47cf369b594be6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=BB=D1=8C=D1=8F=20=D0=A4=D0=B5=D0=B4=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Tue, 30 Jul 2024 15:36:42 +0400 Subject: [PATCH] Emp xlsx report --- .../BusinessLogic/ReportEmployeeLogic.cs | 7 +- .../AbstractSaveToExcelClient.cs | 2 +- .../AbstractSaveToExcelEmployee.cs | 89 +++++++++++++------ .../HelperModels/ExcelInfoEmployee.cs | 6 +- .../Implements/SaveToExcelEmployee.cs | 22 ++--- .../IReportEmployeeLogic.cs | 2 +- .../Controllers/HomeController.cs | 11 +++ .../Views/Home/ReportSearch.cshtml | 3 +- .../Controllers/ClientController.cs | 2 +- .../Controllers/EmployeeController.cs | 15 ++++ 10 files changed, 107 insertions(+), 52 deletions(-) diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ReportEmployeeLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ReportEmployeeLogic.cs index ba34317..974383f 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ReportEmployeeLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ReportEmployeeLogic.cs @@ -78,13 +78,14 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic return ansProductsList; } - public void SaveProductsToExcelFile(ReportProductBindingModel model) + public byte[]? SaveProductsToExcelFile(ReportProductBindingModel model) { - _saveToExcel.CreateReport(new ExcelInfoEmployee + var document = _saveToExcel.CreateReport(new ExcelInfoEmployee { Title = "Список продуктов", - Products = _productStorage.GetFullList(), + ListProduct = GetProducts(model), }); + return document; } public byte[]? SaveProductsToWordFile(ReportProductBindingModel model) diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToExcelClient.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToExcelClient.cs index 313a736..11cc90b 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToExcelClient.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToExcelClient.cs @@ -63,7 +63,7 @@ namespace ElectronicsShopBusinessLogic.OfficePackage InsertCellInWorksheet(new ExcelCellParameters { ColumnName = "A", RowIndex = rowIndex, - Text = "Итого", + Text = "Итого:", StyleInfo = ExcelStyleInfoType.Title }); diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToExcelEmployee.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToExcelEmployee.cs index 0047ec9..4f8738e 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToExcelEmployee.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToExcelEmployee.cs @@ -6,7 +6,7 @@ namespace ElectronicsShopBusinessLogic.OfficePackage { public abstract class AbstractSaveToExcelEmployee { - public void CreateReport(ExcelInfoEmployee info) + public byte[]? CreateReport(ExcelInfoEmployee info) { CreateExcel(info); @@ -18,44 +18,77 @@ namespace ElectronicsShopBusinessLogic.OfficePackage StyleInfo = ExcelStyleInfoType.Title }); - MergeCells(new ExcelMergeParameters - { + MergeCells(new ExcelMergeParameters { CellFromName = "A1", CellToName = "C1" }); uint rowIndex = 2; - foreach (var pc in info.Products) - { - InsertCellInWorksheet(new ExcelCellParameters - { + foreach (var product in info.ListProduct) { + InsertCellInWorksheet(new ExcelCellParameters { ColumnName = "A", RowIndex = rowIndex, - Text = pc.ProductName.ToString(), + Text = product.ProductName, StyleInfo = ExcelStyleInfoType.Text }); rowIndex++; + foreach (var paymeant in product.Values) { + InsertCellInWorksheet(new ExcelCellParameters { + ColumnName = "B", + RowIndex = rowIndex, + Text = "Номер оплаты:", + StyleInfo = ExcelStyleInfoType.TextWithBroder + }); + InsertCellInWorksheet(new ExcelCellParameters { + ColumnName = "C", + RowIndex = rowIndex, + Text = paymeant.PaymeantID.ToString(), + StyleInfo = ExcelStyleInfoType.TextWithBroder + }); + InsertCellInWorksheet(new ExcelCellParameters { + ColumnName = "D", + RowIndex = rowIndex, + Text = "В количестве:", + StyleInfo = ExcelStyleInfoType.TextWithBroder + }); + InsertCellInWorksheet(new ExcelCellParameters { + ColumnName = "E", + RowIndex = rowIndex, + Text = paymeant.ProducCount.ToString(), + StyleInfo = ExcelStyleInfoType.TextWithBroder + }); + InsertCellInWorksheet(new ExcelCellParameters { + ColumnName = "F", + RowIndex = rowIndex, + Text = "Статус оплаты:", + StyleInfo = ExcelStyleInfoType.TextWithBroder + }); + InsertCellInWorksheet(new ExcelCellParameters { + ColumnName = "G", + RowIndex = rowIndex, + Text = paymeant.PaymeantStatus.ToString(), + StyleInfo = ExcelStyleInfoType.TextWithBroder + }); + rowIndex++; + } + InsertCellInWorksheet(new ExcelCellParameters { + ColumnName = "A", + RowIndex = rowIndex, + Text = "Итого:", + StyleInfo = ExcelStyleInfoType.Title + }); - InsertCellInWorksheet(new ExcelCellParameters - { - ColumnName = "B", - RowIndex = rowIndex, - Text = pc.ProductName.ToString(), - StyleInfo = ExcelStyleInfoType.TextWithBroder - }); + InsertCellInWorksheet(new ExcelCellParameters { + ColumnName = "C", + RowIndex = rowIndex, + Text = product.Total.ToString(), + StyleInfo = ExcelStyleInfoType.Title + }); + rowIndex++; + } - InsertCellInWorksheet(new ExcelCellParameters - { - ColumnName = "C", - RowIndex = rowIndex, - Text = pc.Price.ToString(), - StyleInfo = ExcelStyleInfoType.TextWithBroder - }); - - rowIndex++; - } - - SaveExcel(info); + var document = SaveExcel(info); + return document; } protected abstract void CreateExcel(ExcelInfoEmployee info); @@ -64,6 +97,6 @@ namespace ElectronicsShopBusinessLogic.OfficePackage protected abstract void MergeCells(ExcelMergeParameters excelParams); - protected abstract void SaveExcel(ExcelInfoEmployee info); + protected abstract byte[]? SaveExcel(ExcelInfoEmployee info); } } diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/ExcelInfoEmployee.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/ExcelInfoEmployee.cs index 8cca808..2f5ff9e 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/ExcelInfoEmployee.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/HelperModels/ExcelInfoEmployee.cs @@ -4,10 +4,8 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.HelperModels { public class ExcelInfoEmployee { - public string FileName { get; set; } = string.Empty; - public string Title { get; set; } = string.Empty; - public List Products { get; set; } = new(); - } + public List ListProduct { get; set; } = new(); + } } diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/Implements/SaveToExcelEmployee.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/Implements/SaveToExcelEmployee.cs index bd70960..118e8de 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/Implements/SaveToExcelEmployee.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/Implements/SaveToExcelEmployee.cs @@ -17,11 +17,10 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements private Worksheet? _worksheet; - /// - /// Настройка стилей для файла - /// - /// - private static void CreateStyles(WorkbookPart workbookpart) + private MemoryStream _mem = new MemoryStream(); + + // Настройка стилей для файла + private static void CreateStyles(WorkbookPart workbookpart) { var sp = workbookpart.AddNewPart(); sp.Stylesheet = new Stylesheet(); @@ -135,11 +134,7 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements sp.Stylesheet.Append(stylesheetExtensionList); } - /// - /// Получение номера стиля из типа - /// - /// - /// + // Получение номера стиля из типа private static uint GetStyleValue(ExcelStyleInfoType styleInfo) { return styleInfo switch @@ -153,7 +148,7 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements protected override void CreateExcel(ExcelInfoEmployee info) { - _spreadsheetDocument = SpreadsheetDocument.Create(info.FileName, SpreadsheetDocumentType.Workbook); + _spreadsheetDocument = SpreadsheetDocument.Create(_mem, SpreadsheetDocumentType.Workbook); // Создаем книгу (в ней хранятся листы) var workbookpart = _spreadsheetDocument.AddWorkbookPart(); workbookpart.Workbook = new Workbook(); @@ -280,14 +275,15 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements mergeCells.Append(mergeCell); } - protected override void SaveExcel(ExcelInfoEmployee info) + protected override byte[]? SaveExcel(ExcelInfoEmployee info) { if (_spreadsheetDocument == null) { - return; + return null; } _spreadsheetDocument.WorkbookPart!.Workbook.Save(); _spreadsheetDocument.Dispose(); + return _mem.ToArray(); } } } diff --git a/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IReportEmployeeLogic.cs b/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IReportEmployeeLogic.cs index 97df794..854cda3 100644 --- a/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IReportEmployeeLogic.cs +++ b/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IReportEmployeeLogic.cs @@ -12,6 +12,6 @@ namespace ElectronicsShopContracts.BusinessLogicContracts { List GetProducts(ReportProductBindingModel model); byte[]? SaveProductsToWordFile(ReportProductBindingModel model); - void SaveProductsToExcelFile(ReportProductBindingModel model); + byte[]? SaveProductsToExcelFile(ReportProductBindingModel model); } } diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs b/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs index 8457554..b4842db 100644 --- a/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs +++ b/ElectronicsShop/ElectronicsShopEmployeeApp/Controllers/HomeController.cs @@ -352,5 +352,16 @@ namespace ElectronicsShopEmployeeApp.Controllers { return File(fileMemStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Report.docx"); } + + [HttpGet] + public IActionResult CreateExcelReport(string DateFrom, string DateTo) { + var fileMemStream = APIEmployee.GetRequset($"api/Employee/CreateXlsxReport?from={DateFrom}&to={DateTo}"); + + if (fileMemStream == null) { + throw new Exception(" "); + } + + return File(fileMemStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Report.xlsx"); + } } } diff --git a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/ReportSearch.cshtml b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/ReportSearch.cshtml index f50056f..1b433e7 100644 --- a/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/ReportSearch.cshtml +++ b/ElectronicsShop/ElectronicsShopEmployeeApp/Views/Home/ReportSearch.cshtml @@ -27,7 +27,8 @@ diff --git a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/ClientController.cs b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/ClientController.cs index 4ac0fa2..654aa12 100644 --- a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/ClientController.cs +++ b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/ClientController.cs @@ -224,4 +224,4 @@ namespace ElectronicsShopRestAPI.Controllers { } } -} +} \ No newline at end of file diff --git a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/EmployeeController.cs b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/EmployeeController.cs index c77913c..5e838bf 100644 --- a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/EmployeeController.cs +++ b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/EmployeeController.cs @@ -165,5 +165,20 @@ namespace ElectronicsShopRestAPI.Controllers { throw; } } + + [HttpGet] + public byte[]? CreateXlsxReport(string from, string to) { + try { + var document = _reportEmployeeLogic.SaveProductsToExcelFile(new ReportProductBindingModel { + DateFrom = DateTime.Parse(from), + DateTo = DateTime.Parse(to) + }); + return document; + } + catch (Exception ex){ + _logger.LogError(ex, $"Ошибка создания файла"); + throw; + } + } } }