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 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(),
}) ;
}
}
}
}

View File

@ -8,7 +8,7 @@
<ItemGroup>
<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="MigraDocCore.DocumentObjectModel" 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;
@ -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);
}
}

View File

@ -17,10 +17,9 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements
private Worksheet? _worksheet;
/// <summary>
/// Настройка стилей для файла
/// </summary>
/// <param name="workbookpart"></param>
private MemoryStream _mem = new MemoryStream();
// Настройка стилей для файла
private static void CreateStyles(WorkbookPart workbookpart)
{
var sp = workbookpart.AddNewPart<WorkbookStylesPart>();
@ -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();
}
}
}

View File

@ -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<ReportPaymeantProductsViewModel> GetPaymeantProducts(int _clientID);
// получения списка оплат
List<ReportPaymeantsViewModel> 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);
}
}

View File

@ -6,6 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="3.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ElectronicsShopDataModels\ElectronicsShopDataModels.csproj" />
</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.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;
}
}
}
}

View File

@ -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<int, (IProductModel, int)> _productlist;
public MainController(ILogger<MainController> logger, IProductLogic product,
IOrderLogic orderLogic, IPaymeantLogic paymeant) {
IOrderLogic orderLogic, IPaymeantLogic paymeant, IReportClientLogic reportClientLogic) {
_logger = logger;
_product = product;
_order = orderLogic;
_productlist = new Dictionary<int, (IProductModel, int)>();
_paymeant = paymeant;
_reportClientLogic = reportClientLogic;
}
[HttpGet]

Binary file not shown.

View File

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