Emp xlsx report

This commit is contained in:
Илья Федотов 2024-07-30 15:36:42 +04:00
parent 1fd9aaf8c1
commit c12b242073
10 changed files with 107 additions and 52 deletions

View File

@ -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)

View File

@ -63,7 +63,7 @@ namespace ElectronicsShopBusinessLogic.OfficePackage
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "A",
RowIndex = rowIndex,
Text = "Итого",
Text = "Итого:",
StyleInfo = ExcelStyleInfoType.Title
});

View File

@ -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++;
InsertCellInWorksheet(new ExcelCellParameters
{
foreach (var paymeant in product.Values) {
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "B",
RowIndex = rowIndex,
Text = pc.ProductName.ToString(),
Text = "Номер оплаты:",
StyleInfo = ExcelStyleInfoType.TextWithBroder
});
InsertCellInWorksheet(new ExcelCellParameters
{
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "C",
RowIndex = rowIndex,
Text = pc.Price.ToString(),
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 = "C",
RowIndex = rowIndex,
Text = product.Total.ToString(),
StyleInfo = ExcelStyleInfoType.Title
});
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);
}
}

View File

@ -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<ProductViewModel> Products { get; set; } = new();
public List<ReportProductInPaymeantsViewModel> ListProduct { get; set; } = new();
}
}

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>();
@ -135,11 +134,7 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements
sp.Stylesheet.Append(stylesheetExtensionList);
}
/// <summary>
/// Получение номера стиля из типа
/// </summary>
/// <param name="styleInfo"></param>
/// <returns></returns>
// Получение номера стиля из типа
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();
}
}
}

View File

@ -12,6 +12,6 @@ namespace ElectronicsShopContracts.BusinessLogicContracts
{
List<ReportProductInPaymeantsViewModel> GetProducts(ReportProductBindingModel model);
byte[]? SaveProductsToWordFile(ReportProductBindingModel model);
void SaveProductsToExcelFile(ReportProductBindingModel model);
byte[]? SaveProductsToExcelFile(ReportProductBindingModel model);
}
}

View File

@ -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<byte[]>($"api/Employee/CreateXlsxReport?from={DateFrom}&to={DateTo}");
if (fileMemStream == null) {
throw new Exception("Îøèáêà ñîçäàíèÿ îò÷åòà");
}
return File(fileMemStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Report.xlsx");
}
}
}

View File

@ -27,7 +27,8 @@
<div class="col-8">
<a class="btn btn-primary btn-sm" asp-action="CreateWordReport" asp-route-DateFrom="@Model.Item1"
asp-route-DateTo="@Model.Item2" style="background-color:#335a95;">Экспорт отчета в .xlsx</a>
<a class="btn btn-primary btn-sm" asp-action="CreateExcelReport" style="background-color:#04713A;">Экспорт отчета в .docx</a>
<a class="btn btn-primary btn-sm" asp-action="CreateExcelReport" asp-route-DateFrom="@Model.Item1"
asp-route-DateTo="@Model.Item2" style="background-color:#04713A;">Экспорт отчета в .docx</a>
<a class="btn btn-primary btn-sm" asp-action="CreatePdfReport" asp-route-DateFrom="@Model.Item1"
asp-route-DateTo="@Model.Item2" style="background-color:#ad0d09;">отправить на Email отчет в .pdf</a>
</div>

View File

@ -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;
}
}
}
}