RestAPI: new methods + files saving logic was changed.

This commit is contained in:
Yuee Shiness 2023-05-19 18:58:32 +04:00
parent 5f7994beaa
commit 2ebbe9789f
7 changed files with 54 additions and 26 deletions

View File

@ -146,9 +146,9 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
});
}
public void SaveConsignmentsToExcelFile(ReportComponentsBindingModel model)
public byte[] SaveConsignmentsToExcelFile(ReportComponentsBindingModel model)
{
_saveToExcel.CreateReport(new ExcelInfo
return _saveToExcel.CreateReport(new ExcelInfo
{
FileName = model.FileName,
Title = "Consignments list",
@ -156,9 +156,9 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
});
}
public void SaveConsignmentsToWordFile(ReportComponentsBindingModel model)
public byte[] SaveConsignmentsToWordFile(ReportComponentsBindingModel model)
{
_saveToWord.CreateDoc(new WordInfo
return _saveToWord.CreateDoc(new WordInfo
{
FileName = model.FileName,
Title = "Consignments list",

View File

@ -10,8 +10,9 @@ namespace ComputerStoreBusinessLogic.OfficePackage
{
public abstract class AbstractSaveToExcel
{
public void CreateReport(ExcelInfo info)
public byte[] CreateReport(ExcelInfo info)
{
CreateExcel(info);
InsertCellInWorksheet(new ExcelCellParameters
{
@ -70,7 +71,9 @@ namespace ComputerStoreBusinessLogic.OfficePackage
});
rowIndex++;
}
SaveExcel(info);
return SaveExcel(info);
}
protected abstract void CreateExcel(ExcelInfo info);
@ -79,6 +82,6 @@ namespace ComputerStoreBusinessLogic.OfficePackage
protected abstract void MergeCells(ExcelMergeParameters excelParams);
protected abstract void SaveExcel(ExcelInfo info);
protected abstract byte[] SaveExcel(ExcelInfo info);
}
}

View File

@ -10,7 +10,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage
{
public abstract class AbstractSaveToWord
{
public void CreateDoc(WordInfo info)
public byte[] CreateDoc(WordInfo info)
{
CreateWord(info);
CreateParagraph(new WordParagraph
@ -48,11 +48,11 @@ namespace ComputerStoreBusinessLogic.OfficePackage
}
}
SaveWord(info);
return SaveWord(info);
}
protected abstract void CreateWord(WordInfo info);
protected abstract void CreateParagraph(WordParagraph paragraph);
protected abstract void SaveWord(WordInfo info);
protected abstract byte[] SaveWord(WordInfo info);
}
}

View File

@ -18,7 +18,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements
private SpreadsheetDocument? _spreadsheetDocument;
private SharedStringTablePart? _shareStringPart;
private Worksheet? _worksheet;
private MemoryStream? mem;
private static void CreateStyles(WorkbookPart workbookpart)
{
var sp = workbookpart.AddNewPart<WorkbookStylesPart>();
@ -211,8 +211,8 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements
protected override void CreateExcel(ExcelInfo info)
{
_spreadsheetDocument = SpreadsheetDocument.Create(info.FileName,
SpreadsheetDocumentType.Workbook);
mem = new MemoryStream();
_spreadsheetDocument = SpreadsheetDocument.Create(mem, SpreadsheetDocumentType.Workbook);
var workbookpart = _spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
@ -341,14 +341,16 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements
mergeCells.Append(mergeCell);
}
protected override void SaveExcel(ExcelInfo info)
protected override byte[] SaveExcel(ExcelInfo info)
{
if (_spreadsheetDocument == null)
{
return;
return null;
}
_spreadsheetDocument.WorkbookPart!.Workbook.Save();
_spreadsheetDocument.Dispose();
return mem.ToArray();
}
}
}

View File

@ -15,7 +15,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements
{
private WordprocessingDocument? _wordDocument;
private Body? _docBody;
private MemoryStream mem;
private static JustificationValues GetJustificationValues(WordJustificationType type)
{
return type switch
@ -96,21 +96,23 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements
protected override void CreateWord(WordInfo info)
{
_wordDocument = WordprocessingDocument.Create(info.FileName, WordprocessingDocumentType.Document);
mem = new MemoryStream();
_wordDocument = WordprocessingDocument.Create(mem, WordprocessingDocumentType.Document);
MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart();
mainPart.Document = new Document();
_docBody = mainPart.Document.AppendChild(new Body());
}
protected override void SaveWord(WordInfo info)
protected override byte[] SaveWord(WordInfo info)
{
if (_docBody == null || _wordDocument == null)
{
return;
return null;
}
_docBody.AppendChild(CreateSectionProperties());
_wordDocument.MainDocumentPart!.Document.Save();
_wordDocument.Dispose();
return mem.ToArray();
}
}
}

View File

@ -15,7 +15,7 @@ namespace ComputerStoreContracts.BusinessLogicContracts
List<ReportConsignmentsViewModel> GetConsignmentsByComponents(ReportComponentsBindingModel model);
void SavePCsAndProductsToPdfFile(ReportDateBindingModel model);
void SaveConsignmentsToExcelFile(ReportComponentsBindingModel model);
void SaveConsignmentsToWordFile(ReportComponentsBindingModel model);
byte[] SaveConsignmentsToExcelFile(ReportComponentsBindingModel model);
byte[] SaveConsignmentsToWordFile(ReportComponentsBindingModel model);
}
}

View File

@ -2,6 +2,7 @@
using ComputerStoreContracts.BusinessLogicContracts;
using ComputerStoreContracts.SearchModels;
using ComputerStoreContracts.ViewModels;
using DocumentFormat.OpenXml.Packaging;
using Microsoft.AspNetCore.Mvc;
using System.Windows;
@ -508,5 +509,25 @@ namespace ComputerStoreRestAPI.Controllers
{
return _employeeReportLogic.GetConsignmentsByComponents(model);
}
[HttpGet]
public byte[] SaveReportConsignmentsToExcel(ReportComponentsBindingModel model)
{
return _employeeReportLogic.SaveConsignmentsToExcelFile(new ReportComponentsBindingModel
{
FileName = "ReportConsignments",
Components = model.Components
});
}
[HttpGet]
public byte[] SaveReportConsignmentsToWord(ReportComponentsBindingModel model)
{
return _employeeReportLogic.SaveConsignmentsToWordFile(new ReportComponentsBindingModel
{
FileName = "ReportConsignments",
Components = model.Components
});
}
}
}