diff --git a/EventVisitor/EventVisitorClientApp/Controllers/HomeController.cs b/EventVisitor/EventVisitorClientApp/Controllers/HomeController.cs index c7c8186..6d42f34 100644 --- a/EventVisitor/EventVisitorClientApp/Controllers/HomeController.cs +++ b/EventVisitor/EventVisitorClientApp/Controllers/HomeController.cs @@ -5,22 +5,6 @@ using EventVisitorLogic.ViewModels; using Microsoft.AspNetCore.Mvc; using System; using System.Diagnostics; -using System.Net.NetworkInformation; -using System.Net; -using System.Numerics; -using System.Security.Cryptography.Xml; -using System.Xml.Linq; -using Microsoft.Extensions.Logging; -using static System.Runtime.InteropServices.JavaScript.JSType; -using DocumentFormat.OpenXml.Office2010.Excel; -using System.Text; -using System.Security.Cryptography; -using DocumentFormat.OpenXml.Spreadsheet; -using DocumentFormat.OpenXml.Bibliography; -using DocumentFormat.OpenXml.Drawing.Diagrams; -using DocumentFormat.OpenXml.Office2016.Drawing.ChartDrawing; -using DocumentFormat.OpenXml.Wordprocessing; -using Microsoft.EntityFrameworkCore.Metadata.Conventions; namespace EventVisitorClientApp.Controllers { @@ -639,25 +623,25 @@ namespace EventVisitorClientApp.Controllers /// /// /// - public IActionResult GetWordFile() + /// + [HttpPost] + public IActionResult ReportExcel(int id) { - var filePath = "F:\\EventVisitor\\wordfile.docx"; - - if (!System.IO.File.Exists(filePath)) + if (APIClient.Client == null) { - return NotFound(); + throw new Exception(" ? "); } + var reportFilePath = $"F:\\EventVisitor\\Report_{id}_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.xlsx"; + APIClient.PostRequest("api/main/createreporttoexcelfile", new ReportVisitorsBindingModel + { + EventId = id, + FileName = reportFilePath + }); - return new PhysicalFileResult(filePath, "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); + return PhysicalFile(reportFilePath, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + $"Report_{id}.xlsx"); } - - public IActionResult GetExcelFile() - { - return new PhysicalFileResult("F:\\EventVisitor\\excelfile.xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); - } - - [HttpPost] public IActionResult ReportWord(int id) { @@ -666,15 +650,20 @@ namespace EventVisitorClientApp.Controllers throw new Exception(" ? "); } + // + var reportFilePath = $"F:\\EventVisitor\\Report_{id}_{DateTime.Now.ToString("yyyyMMdd_HHmmss")}.docx"; APIClient.PostRequest("api/main/createreporttowordfile", new ReportVisitorsBindingModel { EventId = id, - FileName = "F:\\EventVisitor\\wordfile.docx" + FileName = reportFilePath }); - return RedirectToAction("GetWordFile"); + // + return PhysicalFile(reportFilePath, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", + $"Report_{id}.docx"); } + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] public IActionResult Error(string errorMessage, string returnUrl) { diff --git a/EventVisitor/EventVisitorClientApp/Program.cs b/EventVisitor/EventVisitorClientApp/Program.cs index 534f6fa..01200ad 100644 --- a/EventVisitor/EventVisitorClientApp/Program.cs +++ b/EventVisitor/EventVisitorClientApp/Program.cs @@ -3,11 +3,13 @@ using EventVisitorDatabase.Implements; using EventVisitorLogic.OfficePackage.Implements; using EventVisitorLogic.OfficePackage; using EventVisitorLogic.StoragesContracts; +using EventVisitorLogic.Logic; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllersWithViews(); +builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); builder.Services.AddTransient(); diff --git a/EventVisitor/EventVisitorClientApp/Views/Home/ViewEvent.cshtml b/EventVisitor/EventVisitorClientApp/Views/Home/ViewEvent.cshtml index 1a2344f..5486ada 100644 --- a/EventVisitor/EventVisitorClientApp/Views/Home/ViewEvent.cshtml +++ b/EventVisitor/EventVisitorClientApp/Views/Home/ViewEvent.cshtml @@ -75,7 +75,10 @@ - Отчет Excel о посетителях +
+ + +
diff --git a/EventVisitor/EventVisitorDatabase/Entities/OrganizerEntity.cs b/EventVisitor/EventVisitorDatabase/Entities/OrganizerEntity.cs index d0f8495..0ade3bc 100644 --- a/EventVisitor/EventVisitorDatabase/Entities/OrganizerEntity.cs +++ b/EventVisitor/EventVisitorDatabase/Entities/OrganizerEntity.cs @@ -29,9 +29,6 @@ namespace EventVisitorDatabase.Entities public string Password { get; set; } = string.Empty; [Required] public string Phone { get; set; } = string.Empty; - - - public static OrganizerEntity? Create(OrganizerBindingModel model) { if (model == null) diff --git a/EventVisitor/EventVisitorLogic/BindingModels/ReportVisitorsBindingModel.cs b/EventVisitor/EventVisitorLogic/BindingModels/ReportVisitorsBindingModel.cs index f392ea5..d9dbbef 100644 --- a/EventVisitor/EventVisitorLogic/BindingModels/ReportVisitorsBindingModel.cs +++ b/EventVisitor/EventVisitorLogic/BindingModels/ReportVisitorsBindingModel.cs @@ -11,6 +11,5 @@ namespace EventVisitorLogic.BindingModels { public string FileName { get; set; } = string.Empty; public int EventId { get; set; } - public List Visitors { get; set; } = new(); } } diff --git a/EventVisitor/EventVisitorLogic/OfficePackage/AbstractSaveToExcel.cs b/EventVisitor/EventVisitorLogic/OfficePackage/AbstractSaveToExcel.cs index 1de22b2..441b473 100644 --- a/EventVisitor/EventVisitorLogic/OfficePackage/AbstractSaveToExcel.cs +++ b/EventVisitor/EventVisitorLogic/OfficePackage/AbstractSaveToExcel.cs @@ -20,9 +20,15 @@ namespace EventVisitorLogic.OfficePackage Text = info.Title, StyleInfo = ExcelStyleInfoType.Title }); + MergeCells(new ExcelMergeParameters + { + CellFromName = "A1", + CellToName = "F1" + }); uint rowIndex = 2; foreach (var pc in info.Visitors) { + InsertCellInWorksheet(new ExcelCellParameters { ColumnName = "A", @@ -30,27 +36,47 @@ namespace EventVisitorLogic.OfficePackage Text = pc.Name, StyleInfo = ExcelStyleInfoType.Text }); - InsertCellInWorksheet(new ExcelCellParameters + MergeCells(new ExcelMergeParameters { - ColumnName = "B", - RowIndex = rowIndex, - Text = pc.Email, - StyleInfo = ExcelStyleInfoType.Text + CellFromName = $"A{rowIndex}", + CellToName = $"B{rowIndex}" }); InsertCellInWorksheet(new ExcelCellParameters { ColumnName = "C", RowIndex = rowIndex, - Text = pc.Phone, + Text = pc.Email, StyleInfo = ExcelStyleInfoType.Text }); + MergeCells(new ExcelMergeParameters + { + CellFromName = $"C{rowIndex}", + CellToName = $"D{rowIndex}" + }); InsertCellInWorksheet(new ExcelCellParameters { - ColumnName = "D", + ColumnName = "E", + RowIndex = rowIndex, + Text = pc.Phone, + StyleInfo = ExcelStyleInfoType.Text + }); + MergeCells(new ExcelMergeParameters + { + CellFromName = $"E{rowIndex}", + CellToName = $"F{rowIndex}" + }); + InsertCellInWorksheet(new ExcelCellParameters + { + ColumnName = "G", RowIndex = rowIndex, Text = pc.Status, StyleInfo = ExcelStyleInfoType.Text }); + MergeCells(new ExcelMergeParameters + { + CellFromName = $"G{rowIndex}", + CellToName = $"H{rowIndex}" + }); rowIndex++; } SaveExcel(info); diff --git a/EventVisitor/EventVisitorLogic/OfficePackage/AbstractSaveToWord.cs b/EventVisitor/EventVisitorLogic/OfficePackage/AbstractSaveToWord.cs index 0f7a071..f34e5ff 100644 --- a/EventVisitor/EventVisitorLogic/OfficePackage/AbstractSaveToWord.cs +++ b/EventVisitor/EventVisitorLogic/OfficePackage/AbstractSaveToWord.cs @@ -1,4 +1,6 @@ -using EventVisitorLogic.OfficePackage.HelperEnums; + +using DocumentFormat.OpenXml.Wordprocessing; +using EventVisitorLogic.OfficePackage.HelperEnums; using EventVisitorLogic.OfficePackage.HelperModels; using System; using System.Collections.Generic; @@ -13,21 +15,43 @@ namespace EventVisitorLogic.OfficePackage public void CreateDoc(WordInfo info) { CreateWord(info); + CreateParagraph(new WordParagraph { - Texts = new List<(string, WordTextProperties)> { (info.Title, new WordTextProperties { Bold = true, Size = "24", }) }, + Texts = new List<(string, WordTextProperties)> + { + (info.Title, new WordTextProperties { Bold = true, Size = "24" }) + }, TextProperties = new WordTextProperties { Size = "24", JustificationType = WordJustificationType.Center } }); + + CreateParagraph(new WordParagraph + { + Texts = new List<(string, WordTextProperties)> { ("", new WordTextProperties()) }, // Пустой параграф + TextProperties = new WordTextProperties + { + Size = "24", + JustificationType = WordJustificationType.Center + } + }); + + // Добавление информации о посетителях foreach (var km in info.Visitors) { + // Создание параграфа для каждого посетителя с более красивым оформлением CreateParagraph(new WordParagraph { Texts = new List<(string, WordTextProperties)> - { (km.Name, new WordTextProperties { Size = "20", Bold=true}), (km.Email, new WordTextProperties { Size = "20", Bold=true}), (km.Phone, new WordTextProperties { Size = "20", Bold=true}), (km.Status, new WordTextProperties { Size = "20", Bold=true})}, + { + ($"Имя: {km.Name} ", new WordTextProperties { Size = "20", Bold = true }), + ($"{km.Email} ", new WordTextProperties { Size = "20" }), + ($"{km.Phone} ", new WordTextProperties { Size = "20" }), + ($"{km.Status}", new WordTextProperties { Size = "20" }), + }, TextProperties = new WordTextProperties { Size = "24", @@ -35,11 +59,23 @@ namespace EventVisitorLogic.OfficePackage } }); + // Добавление пробела между записями посетителей + CreateParagraph(new WordParagraph + { + Texts = new List<(string, WordTextProperties)> { ("", new WordTextProperties()) }, // Пустой параграф + TextProperties = new WordTextProperties + { + Size = "24", + JustificationType = WordJustificationType.Center + } + }); } SaveWord(info); } + protected abstract void CreateWord(WordInfo info); protected abstract void CreateParagraph(WordParagraph paragraph); protected abstract void SaveWord(WordInfo info); + } } diff --git a/EventVisitor/EventVisitorLogic/OfficePackage/Implements/SaveToExcel.cs b/EventVisitor/EventVisitorLogic/OfficePackage/Implements/SaveToExcel.cs index 7839fc2..6206273 100644 --- a/EventVisitor/EventVisitorLogic/OfficePackage/Implements/SaveToExcel.cs +++ b/EventVisitor/EventVisitorLogic/OfficePackage/Implements/SaveToExcel.cs @@ -342,6 +342,7 @@ namespace EventVisitorLogic.OfficePackage.Implements }; mergeCells.Append(mergeCell); } + protected override void SaveExcel(ExcelInfo info) { if (_spreadsheetDocument == null) @@ -349,7 +350,8 @@ namespace EventVisitorLogic.OfficePackage.Implements return; } _spreadsheetDocument.WorkbookPart!.Workbook.Save(); - //_spreadsheetDocument.Close(); + _spreadsheetDocument.Dispose(); + _spreadsheetDocument = null; } } } diff --git a/EventVisitor/EventVisitorLogic/OfficePackage/Implements/SaveToWord.cs b/EventVisitor/EventVisitorLogic/OfficePackage/Implements/SaveToWord.cs index a52c800..1c44d2d 100644 --- a/EventVisitor/EventVisitorLogic/OfficePackage/Implements/SaveToWord.cs +++ b/EventVisitor/EventVisitorLogic/OfficePackage/Implements/SaveToWord.cs @@ -107,14 +107,35 @@ namespace EventVisitorLogic.OfficePackage.Implements } _docBody.AppendChild(docParagraph); } + protected override void SaveWord(WordInfo info) { + // Проверьте, инициализированы ли _docBody и _wordDocument if (_docBody == null || _wordDocument == null) { return; } - _docBody.AppendChild(CreateSectionProperties()); - _wordDocument.MainDocumentPart!.Document.Save(); + + try + { + // Добавление свойств секции (если это необходимо) + _docBody.AppendChild(CreateSectionProperties()); + + // Сохранение документа + _wordDocument.MainDocumentPart.Document.Save(); + + } + catch (Exception ex) + { + // Логирование ошибки + Console.WriteLine($"Error saving document: {ex.Message}"); + } + finally + { + // Закрытие документа с помощью Dispose + _wordDocument.Dispose(); // Освобождает ресурсы, связанные с документом + _wordDocument = null; // Сброс ссылки после освобождения + } } }