From 642291fa0a5d47b9b0cdfc9195567a0417075621 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, 9 Jul 2024 19:40:24 +0400 Subject: [PATCH] xlsx reports exports --- .../BusinessLogic/ReportClientLogic.cs | 14 ++++++-------- .../ElectronicsShopBusinessLogic.csproj | 2 +- .../OfficePackage/AbstractSaveToExcelClient.cs | 10 ++++++---- .../Implements/SaveToExcelClient.cs | 16 ++++++++-------- .../IReportClientLogic.cs | 9 +++++---- .../ElectronicsShopContracts.csproj | 4 ++++ .../Controllers/ClientController.cs | 16 +++++++++++++++- .../Controllers/MainController.cs | 5 ++++- .../ElectronicsShopRestAPI/report.xlsx | Bin 0 -> 2869 bytes .../Controllers/HomeController.cs | 12 +++++++----- 10 files changed, 56 insertions(+), 32 deletions(-) create mode 100644 ElectronicsShop/ElectronicsShopRestAPI/report.xlsx diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ReportClientLogic.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ReportClientLogic.cs index 4811c4a..677fa4a 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ReportClientLogic.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/BusinessLogic/ReportClientLogic.cs @@ -1,4 +1,5 @@ -using ElectronicsShopBusinessLogic.OfficePackage; +using DocumentFormat.OpenXml.Packaging; +using ElectronicsShopBusinessLogic.OfficePackage; using ElectronicsShopBusinessLogic.OfficePackage.HelperModels; using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.BusinessLogicContracts; @@ -6,7 +7,6 @@ using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.StorageContracts; using ElectronicsShopContracts.ViewModels; - namespace ElectronicsShopBusinessLogic.BusinessLogic { public class ReportClientLogic : IReportClientLogic @@ -73,14 +73,14 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic return list; } - public void SavePaymeantToExcelFile(ReportBindingModel model, int _clientID) + public byte[] SavePaymeantToExcelFile(ReportBindingModel? model, int _clientID) { - _saveToExcel.CreateReport(new ExcelInfoClient + var document = _saveToExcel.CreateReport(new ExcelInfoClient { - FileName = model.FileName, Title = "Список оплат", PaymeantProducts = GetPaymeantProducts(_clientID) }); + return document; } public void SavePaymeantToWordFile(ReportBindingModel model) @@ -92,7 +92,5 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic ListPaymeant = _paymeantstorage.GetFullList(), }) ; } - - } -} +} \ No newline at end of file diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/ElectronicsShopBusinessLogic.csproj b/ElectronicsShop/ElectronicsShopBusinessLogic/ElectronicsShopBusinessLogic.csproj index 45686b4..c5144c7 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/ElectronicsShopBusinessLogic.csproj +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/ElectronicsShopBusinessLogic.csproj @@ -8,7 +8,7 @@ - + diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToExcelClient.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToExcelClient.cs index 2ba3a11..7a4c20f 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToExcelClient.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/AbstractSaveToExcelClient.cs @@ -1,4 +1,5 @@ -using ElectronicsShopBusinessLogic.OfficePackage.HelperEnums; +using DocumentFormat.OpenXml.Packaging; +using ElectronicsShopBusinessLogic.OfficePackage.HelperEnums; using ElectronicsShopBusinessLogic.OfficePackage.HelperModels; @@ -6,7 +7,7 @@ namespace ElectronicsShopBusinessLogic.OfficePackage { public abstract class AbstractSaveToExcelClient { - public void CreateReport(ExcelInfoClient info) { + public byte[] CreateReport(ExcelInfoClient info) { CreateExcel(info); InsertCellInWorksheet(new ExcelCellParameters { @@ -61,7 +62,8 @@ namespace ElectronicsShopBusinessLogic.OfficePackage }); rowIndex++; } - SaveExcel(info); + var documnet = SaveExcel(info); + return documnet; } // Создание excel-файла @@ -74,6 +76,6 @@ namespace ElectronicsShopBusinessLogic.OfficePackage protected abstract void MergeCells(ExcelMergeParameters excelParams); // Сохранение файла - protected abstract void SaveExcel(ExcelInfoClient info); + protected abstract byte[] SaveExcel(ExcelInfoClient info); } } diff --git a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/Implements/SaveToExcelClient.cs b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/Implements/SaveToExcelClient.cs index 1f6fbfa..c3d4cd2 100644 --- a/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/Implements/SaveToExcelClient.cs +++ b/ElectronicsShop/ElectronicsShopBusinessLogic/OfficePackage/Implements/SaveToExcelClient.cs @@ -17,10 +17,9 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements private Worksheet? _worksheet; - /// - /// Настройка стилей для файла - /// - /// + private MemoryStream _mem = new MemoryStream(); + + // Настройка стилей для файла private static void CreateStyles(WorkbookPart workbookpart) { var sp = workbookpart.AddNewPart(); @@ -149,7 +148,7 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements protected override void CreateExcel(ExcelInfoClient info) { - _spreadsheetDocument = SpreadsheetDocument.Create(info.FileName, SpreadsheetDocumentType.Workbook); + _spreadsheetDocument = SpreadsheetDocument.Create(_mem, SpreadsheetDocumentType.Workbook); // Создаем книгу (в ней хранятся листы) var workbookpart = _spreadsheetDocument.AddWorkbookPart(); workbookpart.Workbook = new Workbook(); @@ -182,7 +181,7 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements sheets.Append(sheet); _worksheet = worksheetPart.Worksheet; - } + } protected override void InsertCellInWorksheet(ExcelCellParameters excelParams) { @@ -276,14 +275,15 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements mergeCells.Append(mergeCell); } - protected override void SaveExcel(ExcelInfoClient info) + protected override byte[] SaveExcel(ExcelInfoClient info) { if (_spreadsheetDocument == null) { - return; + return null; } _spreadsheetDocument.WorkbookPart!.Workbook.Save(); _spreadsheetDocument.Dispose(); + return _mem.ToArray(); } } } diff --git a/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IReportClientLogic.cs b/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IReportClientLogic.cs index ee7cae8..39f65f6 100644 --- a/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IReportClientLogic.cs +++ b/ElectronicsShop/ElectronicsShopContracts/BusinessLogicContracts/IReportClientLogic.cs @@ -1,4 +1,5 @@ -using ElectronicsShopContracts.BindingModels; +using DocumentFormat.OpenXml.Packaging; +using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.ViewModels; using System; using System.Collections.Generic; @@ -14,9 +15,9 @@ namespace ElectronicsShopContracts.BusinessLogicContracts List GetPaymeantProducts(int _clientID); // получения списка оплат List GetPaymeants(ReportBindingModel model); - void SavePaymeantToWordFile(ReportBindingModel model); + void SavePaymeantToWordFile(ReportBindingModel model); - // Сохранение компонент с указанием отчета в .excel - void SavePaymeantToExcelFile(ReportBindingModel model, int _clientID); + // Сохранение компонент с указанием отчета в .excel + byte[] SavePaymeantToExcelFile(ReportBindingModel? model, int _clientID); } } diff --git a/ElectronicsShop/ElectronicsShopContracts/ElectronicsShopContracts.csproj b/ElectronicsShop/ElectronicsShopContracts/ElectronicsShopContracts.csproj index 5c8a174..40b8cdc 100644 --- a/ElectronicsShop/ElectronicsShopContracts/ElectronicsShopContracts.csproj +++ b/ElectronicsShop/ElectronicsShopContracts/ElectronicsShopContracts.csproj @@ -6,6 +6,10 @@ enable + + + + diff --git a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/ClientController.cs b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/ClientController.cs index 9e63387..d3901d5 100644 --- a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/ClientController.cs +++ b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/ClientController.cs @@ -1,4 +1,6 @@ -using ElectronicsShopContracts.BindingModels; +using DocumentFormat.OpenXml.Drawing.Diagrams; +using DocumentFormat.OpenXml.Packaging; +using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.ViewModels; @@ -92,5 +94,17 @@ namespace ElectronicsShopRestAPI.Controllers { throw; } } + + [HttpGet] + public byte[] CreateXlsxReport (int _clientID) { + try { + var document = _reportLogic.SavePaymeantToExcelFile (null, _clientID); + return document; + } + catch (Exception ex) { + _logger.LogError(ex, $"Ошибка создания файла"); + throw; + } + } } } diff --git a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs index 66c75a0..8e81b73 100644 --- a/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs +++ b/ElectronicsShop/ElectronicsShopRestAPI/Controllers/MainController.cs @@ -19,17 +19,20 @@ namespace ElectronicsShopRestAPI.Controllers { private readonly IProductLogic _product; private readonly IOrderLogic _order; private readonly IPaymeantLogic _paymeant; + private readonly IReportClientLogic _reportClientLogic; + //private readonly IMessageInfoLogic _message; private Dictionary _productlist; public MainController(ILogger logger, IProductLogic product, - IOrderLogic orderLogic, IPaymeantLogic paymeant) { + IOrderLogic orderLogic, IPaymeantLogic paymeant, IReportClientLogic reportClientLogic) { _logger = logger; _product = product; _order = orderLogic; _productlist = new Dictionary(); _paymeant = paymeant; + _reportClientLogic = reportClientLogic; } [HttpGet] diff --git a/ElectronicsShop/ElectronicsShopRestAPI/report.xlsx b/ElectronicsShop/ElectronicsShopRestAPI/report.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..a86d406031e7da665601d86b6141c5e9786b1b76 GIT binary patch literal 2869 zcma)82{@E%8y?9tjmW+)V=UR48OvD85H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0Ub3^9^A;#z>(mOqXAz~efhUgh1EtRZl ztJ6ihG_YkW7~cZswz!nL`=U&Vv$9j^^P9C_luG%hdPjJ2=Gg3rNb!itGjbu_XnTz}L=-otEneUI34Pa(GPU)Fl%o;jwH1?8% zqOvO@emheYAzopHXhUN5u#-Estrv@t$IO8Onb!-5B?yS+zMo8@0VC(RI^fi!tHo8G zVq@F%%Ot`t9xGPoGqJ2y_UG7+@X}9xa${v6J8jxuT~EN-E2jV=R|G9lYr7IY9*JL^ zS}OFg>lTiD=8N}jwmMu8?RCtj=`O$g%wW08y_v6a93r12Zt-(E)EFC{^ypnN=^Ran@E>+2WH7%cdZyO3}l`z0+GopZlz zHh+;z1^?Nd^Qd;APh4}Fai|_BDU{-Ijiqy> zPaGR*Z_Tkq=$DypzPtkT``5N1pEm|?T&1i<&H|6CZvD@iWn<_@YhMff$+@}FZ$#K= ztZN95w*IqB{4bkbL)qNA)=-ML)*0Da;N~T|7B~a`)&2h{*o`($"api/Client/CreateXlsxReport?_clientID={APIClient.Client.ID}"); + return File(fileMemStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Report.xlsx"); + } } } \ No newline at end of file