From 33ceb7e60fbf87ebc08c95c04674e25f20a1603a Mon Sep 17 00:00:00 2001 From: MariaBelkina <89656988623@mail.ru> Date: Sun, 22 Dec 2024 21:01:28 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D0=BA.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Reports/DocReport.cs | 25 +++++ .../Reports/WordBuilder.cs | 101 ++++++++++++++++++ .../TradeAndProcurementEnterprice.csproj | 1 + 3 files changed, 127 insertions(+) create mode 100644 TradeAndProcurementEnterprice/TradeAndProcurementEnterprice/Reports/DocReport.cs create mode 100644 TradeAndProcurementEnterprice/TradeAndProcurementEnterprice/Reports/WordBuilder.cs diff --git a/TradeAndProcurementEnterprice/TradeAndProcurementEnterprice/Reports/DocReport.cs b/TradeAndProcurementEnterprice/TradeAndProcurementEnterprice/Reports/DocReport.cs new file mode 100644 index 0000000..8e416fc --- /dev/null +++ b/TradeAndProcurementEnterprice/TradeAndProcurementEnterprice/Reports/DocReport.cs @@ -0,0 +1,25 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TradeAndProcurementEnterprice.Repositories; + +namespace TradeAndProcurementEnterprice.Reports; + +internal class DocReport +{ + private readonly IProductRepository _productRepository; + private readonly IAgentsRepository _agentsRepository; + private readonly IPurchasingCompanyRepository _purchaseCompanyRepository; + private readonly ILogger _logger; + + public DocReport(IProductRepository productRepository, IAgentsRepository agentsRepository, IPurchasingCompanyRepository purchaseCompanyRepository, ILogger logger) + { + _productRepository = productRepository; + _agentsRepository = agentsRepository; + _purchaseCompanyRepository = purchaseCompanyRepository; + _logger = logger; + } +} diff --git a/TradeAndProcurementEnterprice/TradeAndProcurementEnterprice/Reports/WordBuilder.cs b/TradeAndProcurementEnterprice/TradeAndProcurementEnterprice/Reports/WordBuilder.cs new file mode 100644 index 0000000..de79d20 --- /dev/null +++ b/TradeAndProcurementEnterprice/TradeAndProcurementEnterprice/Reports/WordBuilder.cs @@ -0,0 +1,101 @@ +using DocumentFormat.OpenXml; +using DocumentFormat.OpenXml.Drawing.Charts; +using DocumentFormat.OpenXml.Packaging; +using DocumentFormat.OpenXml.Wordprocessing; +using System; +using System.Collections.Generic; +using System.Data.SqlClient; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace TradeAndProcurementEnterprice.Reports; + +internal class WordBuilder +{ + private readonly string _filePath; + + private readonly Document _document; + + private readonly Body _body; + + public WordBuilder(string filePath) + { + if (string.IsNullOrWhiteSpace(filePath)) + { + throw new ArgumentNullException(nameof(filePath)); + } + + if (File.Exists(filePath)) + { + File.Delete(filePath); + } + + _filePath = filePath; + _document = new Document(); + _body = _document.AppendChild(new Body()); + } + + public WordBuilder AddHeader(string header) + { + var paragraph = _body.AppendChild(new Paragraph()); + var run = paragraph.AppendChild(new Run()); + // TODO прописать настройки под жирный текст. + run.AppendChild(new Text(header)); + + return this; + } + + public WordBuilder AddParagraph(string text) + { + var paragraph = _body.AppendChild(new Paragraph()); + var run = paragraph.AppendChild(new Run()); + run.AppendChild(new Text(text)); + + return this; + } + + public WordBuilder AddTable(int[] widths, List data) + { + if (widths == null || widths.Length == 0) { throw new ArgumentNullException(nameof(widths)); } + + if (data == null || data.Count == 0) { throw new ArgumentNullException(nameof(data)); } + + if (data.Any(x => x.Length != widths.Length)) { throw new InvalidOperationException("widths.Length != data.Length"); } + + var table = new Table(); + table.AppendChild(new TableProperties( + new TableBorders( + new TopBorder() { Val = new EnumValue(BorderValues.Single), Size = 12 }, + new BottomBorder() { Val = new EnumValue(BorderValues.Single), Size = 12 }, + new LeftBorder() { Val = new EnumValue(BorderValues.Single), Size = 12 }, + new RightBorder() { Val = new EnumValue(BorderValues.Single), Size = 12 }, + new InsideHorizontalBorder() { Val = new EnumValue(BorderValues.Single), Size = 12 }, + new InsideVerticalBorder() { Val = new EnumValue(BorderValues.Single), Size = 12 } + ))); + + // Заголовок. + var tableRow = new TableRow(); + for (var j = 0; j < widths.Length; ++j) + { + tableRow.Append(new TableCell( + new TableCellProperties(new TableCellWidth() { Width = widths[j].ToString() }), + new Paragraph(new Run(new RunProperties(new Bold()), new Text(data.First()[j]))))); + } + table.Append(tableRow); + + // Данные. + table.Append(data.Skip(1).Select(x => + new TableRow(x.Select(y => new TableCell(new Paragraph(new Run(new Text(y)))))))); + _body.Append(table); + + return this; + } + + public void Build() + { + using var wordDocument = WordprocessingDocument.Create(_filePath, WordprocessingDocumentType.Document); + var mainPart = wordDocument.AddMainDocumentPart(); + mainPart.Document = _document; + } +} diff --git a/TradeAndProcurementEnterprice/TradeAndProcurementEnterprice/TradeAndProcurementEnterprice.csproj b/TradeAndProcurementEnterprice/TradeAndProcurementEnterprice/TradeAndProcurementEnterprice.csproj index a82cacf..4adb731 100644 --- a/TradeAndProcurementEnterprice/TradeAndProcurementEnterprice/TradeAndProcurementEnterprice.csproj +++ b/TradeAndProcurementEnterprice/TradeAndProcurementEnterprice/TradeAndProcurementEnterprice.csproj @@ -11,6 +11,7 @@ +