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; return ansProductsList;
} }
public void SaveProductsToExcelFile(ReportProductBindingModel model) public byte[]? SaveProductsToExcelFile(ReportProductBindingModel model)
{ {
_saveToExcel.CreateReport(new ExcelInfoEmployee var document = _saveToExcel.CreateReport(new ExcelInfoEmployee
{ {
Title = "Список продуктов", Title = "Список продуктов",
Products = _productStorage.GetFullList(), ListProduct = GetProducts(model),
}); });
return document;
} }
public byte[]? SaveProductsToWordFile(ReportProductBindingModel model) public byte[]? SaveProductsToWordFile(ReportProductBindingModel model)

View File

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

View File

@ -6,7 +6,7 @@ namespace ElectronicsShopBusinessLogic.OfficePackage
{ {
public abstract class AbstractSaveToExcelEmployee public abstract class AbstractSaveToExcelEmployee
{ {
public void CreateReport(ExcelInfoEmployee info) public byte[]? CreateReport(ExcelInfoEmployee info)
{ {
CreateExcel(info); CreateExcel(info);
@ -18,44 +18,77 @@ namespace ElectronicsShopBusinessLogic.OfficePackage
StyleInfo = ExcelStyleInfoType.Title StyleInfo = ExcelStyleInfoType.Title
}); });
MergeCells(new ExcelMergeParameters MergeCells(new ExcelMergeParameters {
{
CellFromName = "A1", CellFromName = "A1",
CellToName = "C1" CellToName = "C1"
}); });
uint rowIndex = 2; uint rowIndex = 2;
foreach (var pc in info.Products) foreach (var product in info.ListProduct) {
{ InsertCellInWorksheet(new ExcelCellParameters {
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "A", ColumnName = "A",
RowIndex = rowIndex, RowIndex = rowIndex,
Text = pc.ProductName.ToString(), Text = product.ProductName,
StyleInfo = ExcelStyleInfoType.Text StyleInfo = ExcelStyleInfoType.Text
}); });
rowIndex++; rowIndex++;
foreach (var paymeant in product.Values) {
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "B",
RowIndex = rowIndex,
Text = "Номер оплаты:",
StyleInfo = ExcelStyleInfoType.TextWithBroder
});
InsertCellInWorksheet(new ExcelCellParameters {
ColumnName = "C",
RowIndex = rowIndex,
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 InsertCellInWorksheet(new ExcelCellParameters {
{ ColumnName = "C",
ColumnName = "B", RowIndex = rowIndex,
RowIndex = rowIndex, Text = product.Total.ToString(),
Text = pc.ProductName.ToString(), StyleInfo = ExcelStyleInfoType.Title
StyleInfo = ExcelStyleInfoType.TextWithBroder });
}); rowIndex++;
}
InsertCellInWorksheet(new ExcelCellParameters var document = SaveExcel(info);
{ return document;
ColumnName = "C",
RowIndex = rowIndex,
Text = pc.Price.ToString(),
StyleInfo = ExcelStyleInfoType.TextWithBroder
});
rowIndex++;
}
SaveExcel(info);
} }
protected abstract void CreateExcel(ExcelInfoEmployee info); protected abstract void CreateExcel(ExcelInfoEmployee info);
@ -64,6 +97,6 @@ namespace ElectronicsShopBusinessLogic.OfficePackage
protected abstract void MergeCells(ExcelMergeParameters excelParams); 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 class ExcelInfoEmployee
{ {
public string FileName { get; set; } = string.Empty;
public string Title { 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,11 +17,10 @@ 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>();
sp.Stylesheet = new Stylesheet(); sp.Stylesheet = new Stylesheet();
@ -135,11 +134,7 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements
sp.Stylesheet.Append(stylesheetExtensionList); sp.Stylesheet.Append(stylesheetExtensionList);
} }
/// <summary> // Получение номера стиля из типа
/// Получение номера стиля из типа
/// </summary>
/// <param name="styleInfo"></param>
/// <returns></returns>
private static uint GetStyleValue(ExcelStyleInfoType styleInfo) private static uint GetStyleValue(ExcelStyleInfoType styleInfo)
{ {
return styleInfo switch return styleInfo switch
@ -153,7 +148,7 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements
protected override void CreateExcel(ExcelInfoEmployee info) protected override void CreateExcel(ExcelInfoEmployee 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();
@ -280,14 +275,15 @@ namespace ElectronicsShopBusinessLogic.OfficePackage.Implements
mergeCells.Append(mergeCell); mergeCells.Append(mergeCell);
} }
protected override void SaveExcel(ExcelInfoEmployee info) protected override byte[]? SaveExcel(ExcelInfoEmployee 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

@ -12,6 +12,6 @@ namespace ElectronicsShopContracts.BusinessLogicContracts
{ {
List<ReportProductInPaymeantsViewModel> GetProducts(ReportProductBindingModel model); List<ReportProductInPaymeantsViewModel> GetProducts(ReportProductBindingModel model);
byte[]? SaveProductsToWordFile(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"); 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"> <div class="col-8">
<a class="btn btn-primary btn-sm" asp-action="CreateWordReport" asp-route-DateFrom="@Model.Item1" <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> 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" <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> asp-route-DateTo="@Model.Item2" style="background-color:#ad0d09;">отправить на Email отчет в .pdf</a>
</div> </div>

View File

@ -224,4 +224,4 @@ namespace ElectronicsShopRestAPI.Controllers {
} }
} }
} }

View File

@ -165,5 +165,20 @@ namespace ElectronicsShopRestAPI.Controllers {
throw; 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;
}
}
} }
} }