From faa59fbd6e3329ad62402d8878b08511ef9c2416 Mon Sep 17 00:00:00 2001 From: kotcheshir73 Date: Mon, 19 Dec 2022 12:17:45 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D0=BF=D0=B0=D0=BA=D0=B5=D1=82=D1=83=20word?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/WordDocumentExtension.cs | 12 +---- .../Extensions/WordParagraphExtension.cs | 13 +++++- .../Extensions/WordRunExtension.cs | 19 ++++++++ .../Extensions/WordTableExtension.cs | 12 +++-- .../Interfaces/Word/BuilderWordDocument.cs | 13 ++++++ .../ModelWordDocumentWithHeaderAndTable.cs | 14 ++++++ .../Word/Models/ModelWordDocument.cs | 29 +++++------- ...tPartModel.cs => ModelWordDocumentPart.cs} | 2 +- .../Word/Models/ModelWordParagraph.cs | 7 ++- .../Interfaces/Word/Models/ModelWordTable.cs | 4 +- .../ToolsOffice/Interfaces/Word/PageSizes.cs | 44 +++++++++++++++++++ 11 files changed, 132 insertions(+), 37 deletions(-) create mode 100644 DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/Cases/ModelWordDocumentWithHeaderAndTable.cs rename DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/{IWordDocumentPartModel.cs => ModelWordDocumentPart.cs} (81%) create mode 100644 DepartmentPortal/Common/ToolsOffice/Interfaces/Word/PageSizes.cs diff --git a/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/Extensions/WordDocumentExtension.cs b/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/Extensions/WordDocumentExtension.cs index 4132b88..8721179 100644 --- a/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/Extensions/WordDocumentExtension.cs +++ b/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/Extensions/WordDocumentExtension.cs @@ -39,15 +39,7 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions var pageMargin = new PageMargin(); if (model.PageMarginBottom.HasValue) { - pageMargin.Bottom = model.PageMarginBottom.Value; - } - if (model.PageMarginFooter != null) - { - pageMargin.Footer = new UInt32Value(Convert.ToUInt32(model.PageMarginFooter.Value)); - } - if (model.PageMarginGutter != null) - { - pageMargin.Gutter = new UInt32Value(Convert.ToUInt32(model.PageMarginGutter.Value)); + pageMargin.Bottom = Convert.ToInt32(model.PageMarginBottom.Value); } if (model.PageMarginLeft != null) { @@ -59,7 +51,7 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions } if (model.PageMarginTop.HasValue) { - pageMargin.Top = model.PageMarginTop.Value; + pageMargin.Top = Convert.ToInt32(model.PageMarginTop.Value); } properties.AppendChild(pageMargin); diff --git a/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/Extensions/WordParagraphExtension.cs b/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/Extensions/WordParagraphExtension.cs index 5a7e3d0..7ad02db 100644 --- a/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/Extensions/WordParagraphExtension.cs +++ b/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/Extensions/WordParagraphExtension.cs @@ -1,6 +1,7 @@ using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Wordprocessing; using System; +using ToolsModule.ManagmentExtension; using ToolsOffice.Interfaces.Word.Models; namespace ToolsOffice.Implements.WordOpenXML.Extensions @@ -20,7 +21,11 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions } var paragraph = new Paragraph(); paragraph.AddParagraphProperties(model); - if (model.WordTexts != null) + if (model.Text.IsNotEmpty()) + { + paragraph.AddRun(model.Text); + } + else if (model.WordTexts != null) { foreach (var text in model.WordTexts) { @@ -43,7 +48,11 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions } var paragraph = new Paragraph(); paragraph.AddParagraphProperties(model); - if (model.WordTexts != null) + if (model.Text.IsNotEmpty()) + { + paragraph.AddRun(model.Text); + } + else if (model.WordTexts != null) { foreach (var text in model.WordTexts) { diff --git a/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/Extensions/WordRunExtension.cs b/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/Extensions/WordRunExtension.cs index 3935661..04b6a88 100644 --- a/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/Extensions/WordRunExtension.cs +++ b/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/Extensions/WordRunExtension.cs @@ -1,5 +1,6 @@ using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Wordprocessing; +using ToolsModule.ManagmentExtension; using ToolsOffice.Interfaces.Word.Models; namespace ToolsOffice.Implements.WordOpenXML.Extensions @@ -34,6 +35,24 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions paragraph.AppendChild(docRun); } + /// + /// Добавление текста в абзац + /// + /// + /// + public static void AddRun(this Paragraph paragraph, string text) + { + if (text.IsEmpty()) + { + return; + } + var docRun = new Run(); + { + docRun.AppendChild(new Text { Text = text, Space = SpaceProcessingModeValues.Preserve }); + } + paragraph.AppendChild(docRun); + } + /// /// Добавление свойств текста /// diff --git a/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/Extensions/WordTableExtension.cs b/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/Extensions/WordTableExtension.cs index 4df55a4..e4e64a9 100644 --- a/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/Extensions/WordTableExtension.cs +++ b/DepartmentPortal/Common/ToolsOffice/Implements/WordOpenXML/Extensions/WordTableExtension.cs @@ -37,6 +37,10 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions /// private static void CreateRows(this ModelWordTable model) { + if (model == null || model.Data == null || model.Data.Count == 0) + { + return; + } model.Rows = new Queue(); var headerRowsCount = model.Headers.Select(x => x.RowIndex).Distinct().Count(); @@ -48,20 +52,20 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions { header.Height = model.RowsHeight[rowIndex]; } - for (int i = 0; i < model.Data.GetLength(1); ++i) + for (int i = 0; i < model.Data[0].Length; ++i) { header.Cells.Enqueue(CreateHeaderCell(model, rowIndex, i)); } model.Rows.Enqueue(header); } - for (int i = 0; i < model.Data.Length; ++i) + for (int i = 0; i < model.Data.Count; ++i) { var row = new ModelWordTableRow(); if (model.RowsHeight.ContainsKey(i + headerRowsCount)) { row.Height = model.RowsHeight[i + headerRowsCount]; } - for (int j = 0; j < model.Data.GetLength(1); ++j) + for (int j = 0; j < model.Data[0].Length; ++j) { row.Cells.Enqueue(CreateCell(model, i, j)); } @@ -241,7 +245,7 @@ namespace ToolsOffice.Implements.WordOpenXML.Extensions }; paragraph.WordTexts.Enqueue(new ModelWordText { - Text = model.Data[rowIndex, columnIndex] + Text = model.Data[rowIndex][columnIndex] }); cell.Texts.Enqueue(paragraph); } diff --git a/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/BuilderWordDocument.cs b/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/BuilderWordDocument.cs index 0122b9f..fbdf8a3 100644 --- a/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/BuilderWordDocument.cs +++ b/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/BuilderWordDocument.cs @@ -1,10 +1,23 @@ using System.IO; using ToolsOffice.Interfaces.Word.Models; +using ToolsOffice.Interfaces.Word.Models.Cases; namespace ToolsOffice.Interfaces.Word { public abstract class BuilderWordDocument { + public Stream CreateDocumentWithTable(ModelWordDocumentWithHeaderAndTable model) + { + if (model == null || model.Document == null || model.Header == null || model.Table == null) + { + return null; + } + CreateDocument(model.Document); + CreateParagraph(model.Header); + CreateTable(model.Table); + return SaveDocument(model.Document); + } + /// /// Создание документа /// diff --git a/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/Cases/ModelWordDocumentWithHeaderAndTable.cs b/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/Cases/ModelWordDocumentWithHeaderAndTable.cs new file mode 100644 index 0000000..24bc2ef --- /dev/null +++ b/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/Cases/ModelWordDocumentWithHeaderAndTable.cs @@ -0,0 +1,14 @@ +namespace ToolsOffice.Interfaces.Word.Models.Cases +{ + /// + /// Модель для создания простого документа с шапкой и таблицей + /// + public class ModelWordDocumentWithHeaderAndTable + { + public ModelWordDocument Document { get; set; } + + public ModelWordParagraph Header { get; set; } + + public ModelWordTable Table { get; set; } + } +} \ No newline at end of file diff --git a/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/ModelWordDocument.cs b/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/ModelWordDocument.cs index d49c8f6..443b3f7 100644 --- a/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/ModelWordDocument.cs +++ b/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/ModelWordDocument.cs @@ -5,32 +5,27 @@ /// public class ModelWordDocument { - /// - /// Высота страницы - /// - public int? PageSizeHeight { get; set; } - - /// - /// Ширина страницы - /// - public int? PageSizeWidth { get; set; } - /// /// Ориентация страницы /// public TypeWordPageOrientation? WordPageOrientation { get; set; } + /// + /// Высота страницы + /// + public double? PageSizeHeight { get; set; } - public int? PageMarginFooter { get; set; } + /// + /// Ширина страницы + /// + public double? PageSizeWidth { get; set; } - public int? PageMarginGutter { get; set; } + public double? PageMarginBottom { get; set; } - public int? PageMarginBottom { get; set; } + public double? PageMarginTop { get; set; } - public int? PageMarginTop { get; set; } + public double? PageMarginLeft { get; set; } - public int? PageMarginLeft { get; set; } - - public int? PageMarginRight { get; set; } + public double? PageMarginRight { get; set; } } } \ No newline at end of file diff --git a/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/IWordDocumentPartModel.cs b/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/ModelWordDocumentPart.cs similarity index 81% rename from DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/IWordDocumentPartModel.cs rename to DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/ModelWordDocumentPart.cs index 64d17c9..26b3459 100644 --- a/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/IWordDocumentPartModel.cs +++ b/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/ModelWordDocumentPart.cs @@ -3,5 +3,5 @@ /// /// Модель данных при создании элемента документа (абзац, таблица) /// - public interface IWordDocumentPartModel { } + public class ModelWordDocumentPart { } } \ No newline at end of file diff --git a/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/ModelWordParagraph.cs b/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/ModelWordParagraph.cs index 5267ea9..d0cf9a5 100644 --- a/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/ModelWordParagraph.cs +++ b/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/ModelWordParagraph.cs @@ -5,8 +5,13 @@ namespace ToolsOffice.Interfaces.Word.Models /// /// Модель описания абзаца документа /// - public class ModelWordParagraph : IWordDocumentPartModel + public class ModelWordParagraph : ModelWordDocumentPart { + /// + /// Текст одним стилем форматирования + /// + public string Text { get; set; } + /// /// Набор текстов /// diff --git a/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/ModelWordTable.cs b/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/ModelWordTable.cs index 5d3ab52..79ef7ac 100644 --- a/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/ModelWordTable.cs +++ b/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/Models/ModelWordTable.cs @@ -5,7 +5,7 @@ namespace ToolsOffice.Interfaces.Word.Models /// /// Модель описания таблицы документа /// - public class ModelWordTable : IWordDocumentPartModel + public class ModelWordTable : ModelWordDocumentPart { /// /// Информация по ширине колонок (номер колонки, ширина колонки), отсчет с 0 @@ -30,7 +30,7 @@ namespace ToolsOffice.Interfaces.Word.Models /// /// Данные /// - public string[,] Data { get; set; } + public List Data { get; set; } /// /// Иной вывод первой строки diff --git a/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/PageSizes.cs b/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/PageSizes.cs new file mode 100644 index 0000000..bd4f745 --- /dev/null +++ b/DepartmentPortal/Common/ToolsOffice/Interfaces/Word/PageSizes.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using System.Linq; + +namespace ToolsOffice.Interfaces.Word +{ + public class PageSizes + { + private static readonly + Dictionary _sizes = new() + { + { "А4", (27.94, 21.59, 2, 2, 3, 1.5) } + }; + + public static List Sizes => _sizes.Keys.ToList(); + + private readonly bool _hasKey; + + private readonly string _key; + + public PageSizes(string key) + { + _hasKey = _sizes.ContainsKey(key); + _key = key; + } + + /// + /// Высота страницы + /// + public double? PageSizeHeight => _hasKey ? _sizes[_key].Height : null; + + /// + /// Ширина страницы + /// + public double? PageSizeWidth => _hasKey ? _sizes[_key].Width : null; + + public double? PageMarginBottom => _hasKey ? _sizes[_key].MarginBottom : null; + + public double? PageMarginTop => _hasKey ? _sizes[_key].MarginTop : null; + + public double? PageMarginLeft => _hasKey ? _sizes[_key].MarginLeft : null; + + public double? PageMarginRight => _hasKey ? _sizes[_key].MarginRigth : null; + } +} \ No newline at end of file