xlsx reports exports

This commit is contained in:
Илья Федотов 2024-07-09 19:40:24 +04:00
parent 560194eb95
commit 642291fa0a
10 changed files with 56 additions and 32 deletions

View File

@ -1,4 +1,5 @@
using ElectronicsShopBusinessLogic.OfficePackage; using DocumentFormat.OpenXml.Packaging;
using ElectronicsShopBusinessLogic.OfficePackage;
using ElectronicsShopBusinessLogic.OfficePackage.HelperModels; using ElectronicsShopBusinessLogic.OfficePackage.HelperModels;
using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.BusinessLogicContracts;
@ -6,7 +7,6 @@ using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.StorageContracts; using ElectronicsShopContracts.StorageContracts;
using ElectronicsShopContracts.ViewModels; using ElectronicsShopContracts.ViewModels;
namespace ElectronicsShopBusinessLogic.BusinessLogic namespace ElectronicsShopBusinessLogic.BusinessLogic
{ {
public class ReportClientLogic : IReportClientLogic public class ReportClientLogic : IReportClientLogic
@ -73,14 +73,14 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
return list; 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 = "Список оплат", Title = "Список оплат",
PaymeantProducts = GetPaymeantProducts(_clientID) PaymeantProducts = GetPaymeantProducts(_clientID)
}); });
return document;
} }
public void SavePaymeantToWordFile(ReportBindingModel model) public void SavePaymeantToWordFile(ReportBindingModel model)
@ -92,7 +92,5 @@ namespace ElectronicsShopBusinessLogic.BusinessLogic
ListPaymeant = _paymeantstorage.GetFullList(), ListPaymeant = _paymeantstorage.GetFullList(),
}) ; }) ;
} }
} }
} }

View File

@ -8,7 +8,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.2" /> <PackageReference Include="DocumentFormat.OpenXml" Version="3.0.2" />
<PackageReference Include="MailKit" Version="4.6.0" /> <PackageReference Include="MailKit" Version="4.7.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="MigraDocCore.DocumentObjectModel" Version="1.3.63" /> <PackageReference Include="MigraDocCore.DocumentObjectModel" Version="1.3.63" />
<PackageReference Include="MigraDocCore.Rendering" Version="1.3.63" /> <PackageReference Include="MigraDocCore.Rendering" Version="1.3.63" />

View File

@ -1,4 +1,5 @@
using ElectronicsShopBusinessLogic.OfficePackage.HelperEnums; using DocumentFormat.OpenXml.Packaging;
using ElectronicsShopBusinessLogic.OfficePackage.HelperEnums;
using ElectronicsShopBusinessLogic.OfficePackage.HelperModels; using ElectronicsShopBusinessLogic.OfficePackage.HelperModels;
@ -6,7 +7,7 @@ namespace ElectronicsShopBusinessLogic.OfficePackage
{ {
public abstract class AbstractSaveToExcelClient public abstract class AbstractSaveToExcelClient
{ {
public void CreateReport(ExcelInfoClient info) { public byte[] CreateReport(ExcelInfoClient info) {
CreateExcel(info); CreateExcel(info);
InsertCellInWorksheet(new ExcelCellParameters { InsertCellInWorksheet(new ExcelCellParameters {
@ -61,7 +62,8 @@ namespace ElectronicsShopBusinessLogic.OfficePackage
}); });
rowIndex++; rowIndex++;
} }
SaveExcel(info); var documnet = SaveExcel(info);
return documnet;
} }
// Создание excel-файла // Создание excel-файла
@ -74,6 +76,6 @@ namespace ElectronicsShopBusinessLogic.OfficePackage
protected abstract void MergeCells(ExcelMergeParameters excelParams); protected abstract void MergeCells(ExcelMergeParameters excelParams);
// Сохранение файла // Сохранение файла
protected abstract void SaveExcel(ExcelInfoClient info); protected abstract byte[] SaveExcel(ExcelInfoClient info);
} }
} }

View File

@ -17,10 +17,9 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements
private Worksheet? _worksheet; private Worksheet? _worksheet;
/// <summary> private MemoryStream _mem = new MemoryStream();
/// Настройка стилей для файла
/// </summary> // Настройка стилей для файла
/// <param name="workbookpart"></param>
private static void CreateStyles(WorkbookPart workbookpart) private static void CreateStyles(WorkbookPart workbookpart)
{ {
var sp = workbookpart.AddNewPart<WorkbookStylesPart>(); var sp = workbookpart.AddNewPart<WorkbookStylesPart>();
@ -149,7 +148,7 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements
protected override void CreateExcel(ExcelInfoClient info) protected override void CreateExcel(ExcelInfoClient info)
{ {
_spreadsheetDocument = SpreadsheetDocument.Create(info.FileName, SpreadsheetDocumentType.Workbook); _spreadsheetDocument = SpreadsheetDocument.Create(_mem, SpreadsheetDocumentType.Workbook);
// Создаем книгу (в ней хранятся листы) // Создаем книгу (в ней хранятся листы)
var workbookpart = _spreadsheetDocument.AddWorkbookPart(); var workbookpart = _spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook(); workbookpart.Workbook = new Workbook();
@ -182,7 +181,7 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements
sheets.Append(sheet); sheets.Append(sheet);
_worksheet = worksheetPart.Worksheet; _worksheet = worksheetPart.Worksheet;
} }
protected override void InsertCellInWorksheet(ExcelCellParameters excelParams) protected override void InsertCellInWorksheet(ExcelCellParameters excelParams)
{ {
@ -276,14 +275,15 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements
mergeCells.Append(mergeCell); mergeCells.Append(mergeCell);
} }
protected override void SaveExcel(ExcelInfoClient info) protected override byte[] SaveExcel(ExcelInfoClient info)
{ {
if (_spreadsheetDocument == null) if (_spreadsheetDocument == null)
{ {
return; return null;
} }
_spreadsheetDocument.WorkbookPart!.Workbook.Save(); _spreadsheetDocument.WorkbookPart!.Workbook.Save();
_spreadsheetDocument.Dispose(); _spreadsheetDocument.Dispose();
return _mem.ToArray();
} }
} }
} }

View File

@ -1,4 +1,5 @@
using ElectronicsShopContracts.BindingModels; using DocumentFormat.OpenXml.Packaging;
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.ViewModels; using ElectronicsShopContracts.ViewModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -14,9 +15,9 @@ namespace ElectronicsShopContracts.BusinessLogicContracts
List<ReportPaymeantProductsViewModel> GetPaymeantProducts(int _clientID); List<ReportPaymeantProductsViewModel> GetPaymeantProducts(int _clientID);
// получения списка оплат // получения списка оплат
List<ReportPaymeantsViewModel> GetPaymeants(ReportBindingModel model); List<ReportPaymeantsViewModel> GetPaymeants(ReportBindingModel model);
void SavePaymeantToWordFile(ReportBindingModel model); void SavePaymeantToWordFile(ReportBindingModel model);
// Сохранение компонент с указанием отчета в .excel // Сохранение компонент с указанием отчета в .excel
void SavePaymeantToExcelFile(ReportBindingModel model, int _clientID); byte[] SavePaymeantToExcelFile(ReportBindingModel? model, int _clientID);
} }
} }

View File

@ -6,6 +6,10 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.2" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ElectronicsShopDataModels\ElectronicsShopDataModels.csproj" /> <ProjectReference Include="..\ElectronicsShopDataModels\ElectronicsShopDataModels.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -1,4 +1,6 @@
using ElectronicsShopContracts.BindingModels; using DocumentFormat.OpenXml.Drawing.Diagrams;
using DocumentFormat.OpenXml.Packaging;
using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.SearchModels;
using ElectronicsShopContracts.ViewModels; using ElectronicsShopContracts.ViewModels;
@ -92,5 +94,17 @@ namespace ElectronicsShopRestAPI.Controllers {
throw; throw;
} }
} }
[HttpGet]
public byte[] CreateXlsxReport (int _clientID) {
try {
var document = _reportLogic.SavePaymeantToExcelFile (null, _clientID);
return document;
}
catch (Exception ex) {
_logger.LogError(ex, $"Ошибка создания файла");
throw;
}
}
} }
} }

View File

@ -19,17 +19,20 @@ namespace ElectronicsShopRestAPI.Controllers {
private readonly IProductLogic _product; private readonly IProductLogic _product;
private readonly IOrderLogic _order; private readonly IOrderLogic _order;
private readonly IPaymeantLogic _paymeant; private readonly IPaymeantLogic _paymeant;
private readonly IReportClientLogic _reportClientLogic;
//private readonly IMessageInfoLogic _message; //private readonly IMessageInfoLogic _message;
private Dictionary<int, (IProductModel, int)> _productlist; private Dictionary<int, (IProductModel, int)> _productlist;
public MainController(ILogger<MainController> logger, IProductLogic product, public MainController(ILogger<MainController> logger, IProductLogic product,
IOrderLogic orderLogic, IPaymeantLogic paymeant) { IOrderLogic orderLogic, IPaymeantLogic paymeant, IReportClientLogic reportClientLogic) {
_logger = logger; _logger = logger;
_product = product; _product = product;
_order = orderLogic; _order = orderLogic;
_productlist = new Dictionary<int, (IProductModel, int)>(); _productlist = new Dictionary<int, (IProductModel, int)>();
_paymeant = paymeant; _paymeant = paymeant;
_reportClientLogic = reportClientLogic;
} }
[HttpGet] [HttpGet]

Binary file not shown.

View File

@ -1,3 +1,4 @@
using DocumentFormat.OpenXml.Bibliography;
using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.BindingModels;
using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.BusinessLogicContracts;
using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.SearchModels;
@ -8,6 +9,7 @@ using ElectronicsShopUserApp.Models;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Diagnostics; using System.Diagnostics;
using System.Net.Mime;
using System.Reflection; using System.Reflection;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -308,10 +310,10 @@ namespace ElectronicsShopUserApp.Controllers {
return View(tuple); return View(tuple);
} }
//todo запрос на создание файла/выгрузку [HttpGet]
[HttpPost] public IActionResult CreateExcelReport() {
public void CreateExcelReport() { var fileMemStream = APIClient.GetRequset<byte[]>($"api/Client/CreateXlsxReport?_clientID={APIClient.Client.ID}");
return File(fileMemStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Report.xlsx");
} }
} }
} }