From 34bf359bc0d3947fa55555faa87e13089a2df90a Mon Sep 17 00:00:00 2001 From: bulatova_karina Date: Mon, 30 Sep 2024 21:27:55 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D0=B1=D0=B5=D1=80=D0=B5=D0=B3?= =?UTF-8?q?=D0=B8=D1=82=D0=B5=20=D1=81=D0=BB=D0=B5=D0=B7=D1=8B=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D1=81=D0=BB=D0=B5=D0=B4=D1=83=D1=8E=D1=89=D0=B8?= =?UTF-8?q?=D1=85=20=D0=BB=D0=B0=D0=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NonVisualComponents/WordText.cs | 138 +++++++----------- COP/WinForms/FormWord.cs | 12 +- 2 files changed, 58 insertions(+), 92 deletions(-) diff --git a/COP/Components/NonVisualComponents/WordText.cs b/COP/Components/NonVisualComponents/WordText.cs index cbe643a..8ea94d7 100644 --- a/COP/Components/NonVisualComponents/WordText.cs +++ b/COP/Components/NonVisualComponents/WordText.cs @@ -1,26 +1,14 @@ using System; -using System.Collections.Generic; using System.ComponentModel; -using System.Diagnostics; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Wordprocessing; -using DocumentFormat.OpenXml; using Components.SupportClasses; -using Aspose.Words; -using Document = DocumentFormat.OpenXml.Wordprocessing.Document; -using Paragraph = DocumentFormat.OpenXml.Wordprocessing.Paragraph; -using Run = DocumentFormat.OpenXml.Wordprocessing.Run; -using Body = DocumentFormat.OpenXml.Wordprocessing.Body; +using DocumentFormat.OpenXml; namespace Components.NonVisualComponents { public partial class WordText : Component { - private WordprocessingDocument? _wordDocument; - private Body? _docBody; public WordText() { InitializeComponent(); @@ -29,104 +17,81 @@ namespace Components.NonVisualComponents public WordText(IContainer container) { container.Add(this); - InitializeComponent(); } + public void CreateWordText(LargeText largeText) { if (string.IsNullOrEmpty(largeText.FilePath) || string.IsNullOrEmpty(largeText.DocumentTitle) || !CheckData(largeText.TextData)) { throw new Exception("Не все данные заполнены"); } - _wordDocument = WordprocessingDocument.Create(largeText.FilePath, WordprocessingDocumentType.Document); - //вытаскиваем главную часть из вордовского документа - MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart(); + using (var wordDocument = WordprocessingDocument.Create(largeText.FilePath, WordprocessingDocumentType.Document)) + { + // Вытаскиваем главную часть из вордовского документа + MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); + mainPart.Document = new Document(); - mainPart.Document = new Document(); + // Генерируем тело основной части документа + Body docBody = mainPart.Document.AppendChild(new Body()); - //генерируем тело основной части документа - _docBody = mainPart.Document.AppendChild(new Body()); - - _wordDocument.Clone(); - - AddText(largeText); + // Добавляем текст + AddText(docBody, largeText); + } } - private void AddText(LargeText largeText) + private void AddText(Body docBody, LargeText largeText) { - using (var document = WordprocessingDocument.Open(largeText.FilePath, true)) + #region Создание заголовка + ParagraphProperties paragraphProperties = new(); + paragraphProperties.AppendChild(new Justification { - var doc = document.MainDocumentPart.Document; + Val = JustificationValues.Center + }); + paragraphProperties.AppendChild(new Indentation()); - #region Создание заголовка + Paragraph header = new(); + header.AppendChild(paragraphProperties); - ParagraphProperties paragraphProperties = new(); + var docRun = new Run(); + var properties = new RunProperties(); + properties.AppendChild(new FontSize + { + Val = "48" + }); + properties.AppendChild(new Bold()); + docRun.AppendChild(properties); + docRun.AppendChild(new Text(largeText.DocumentTitle)); + header.AppendChild(docRun); + docBody.Append(header); + #endregion - paragraphProperties.AppendChild(new Justification + #region Создание текста + for (int i = 0; i < largeText.TextData.Length; i++) + { + ParagraphProperties paragraphProperties2 = new(); + paragraphProperties2.AppendChild(new Justification { - Val = JustificationValues.Center + Val = JustificationValues.Both }); + paragraphProperties2.AppendChild(new Indentation()); - paragraphProperties.AppendChild(new Indentation()); + Paragraph text = new(); + text.AppendChild(paragraphProperties2); - Paragraph header = new(); - - header.AppendChild(paragraphProperties); - - var docRun = new Run(); - - var properties = new RunProperties(); - - properties.AppendChild(new FontSize + var docRun2 = new Run(); + var properties2 = new RunProperties(); + properties2.AppendChild(new FontSize { - Val = "48" + Val = "24" }); - - properties.AppendChild(new Bold()); - - docRun.AppendChild(properties); - - docRun.AppendChild(new Text(largeText.DocumentTitle)); - - header.AppendChild(docRun); - doc.Body.Append(header); - #endregion - - #region Создание текста - for (int i = 0; i < largeText.TextData.Length; i++) - { - ParagraphProperties paragraphProperties2 = new(); - - paragraphProperties2.AppendChild(new Justification - { - Val = JustificationValues.Both - }); - - paragraphProperties2.AppendChild(new Indentation()); - - Paragraph text = new(); - - text.AppendChild(paragraphProperties2); - - var docRun2 = new Run(); - - var properties2 = new RunProperties(); - - properties2.AppendChild(new FontSize - { - Val = "24" - }); - - docRun2.AppendChild(properties2); - docRun2.AppendChild(new Text(largeText.TextData[i])); - - text.AppendChild(docRun2); - doc.Body.Append(text); - } - #endregion - doc.Save(); + docRun2.AppendChild(properties2); + docRun2.AppendChild(new Text(largeText.TextData[i])); + text.AppendChild(docRun2); + docBody.Append(text); } + #endregion } bool CheckData(string[] data) @@ -135,7 +100,6 @@ namespace Components.NonVisualComponents { if (string.IsNullOrEmpty(data[i])) return false; } - return true; } } diff --git a/COP/WinForms/FormWord.cs b/COP/WinForms/FormWord.cs index 7b60840..e66082b 100644 --- a/COP/WinForms/FormWord.cs +++ b/COP/WinForms/FormWord.cs @@ -48,6 +48,7 @@ namespace WinForms } } + private void button2_Click(object sender, EventArgs e) { List mergedColumns = new() @@ -59,10 +60,10 @@ namespace WinForms List columnDefinitions = new List { - new ColumnDefinition { Header = "Идентификатор", PropertyName = "Id", Weight = 35 }, - new ColumnDefinition { Header = "Пол", PropertyName = "Gender", Weight = 35 }, - new ColumnDefinition { Header = "Имя", PropertyName = "Name", Weight = 10 }, - new ColumnDefinition { Header = "Фамилия", PropertyName = "LastName", Weight = 20 }, + new ColumnDefinition { Header = "Работа", PropertyName = "Work", Weight = 35 }, + new ColumnDefinition { Header = "", PropertyName = "Education1", Weight = 35 }, + new ColumnDefinition { Header = "", PropertyName = "Education2", Weight = 10 }, + new ColumnDefinition { Header = "Фамилия", PropertyName = "LastName", Weight = 20 } }; @@ -70,7 +71,8 @@ namespace WinForms { new ColumnDefinition { Header = "Возраст", PropertyName = "Age", Weight = 20 }, new ColumnDefinition { Header = "Опыт", PropertyName = "Experience", Weight = 20 }, - new ColumnDefinition { Header = "Должность", PropertyName = "Position", Weight = 20 } + new ColumnDefinition { Header = "Должность", PropertyName = "Position", Weight = 20 }, + new ColumnDefinition { Header = "Фамилия", PropertyName = "LastName", Weight = 20 } }; List data = new List