From 5335b9bf4038177fe93b1378db11745e2c802f1f Mon Sep 17 00:00:00 2001
From: ElEgEv <112943269+ElEgEv@users.noreply.github.com>
Date: Fri, 6 Oct 2023 00:47:23 +0400
Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B4=D0=B2=D0=B8=D0=B3?=
=?UTF-8?q?=D0=B0=D1=8E=D1=81=D1=8C...?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../VisualComponentsForm/FormWord.Designer.cs | 76 +++++++++++
.../VisualComponentsForm/FormWord.cs | 61 +++++++++
.../VisualComponentsForm/FormWord.resx | 123 ++++++++++++++++++
.../VisualComponentsForm/Program.cs | 2 +-
.../Components/ComponentWord.cs | 93 ++++++++++++-
.../Components/SupportClasses/SimpleTable.cs | 24 ++++
6 files changed, 377 insertions(+), 2 deletions(-)
create mode 100644 VisualComponentsForm/VisualComponentsForm/FormWord.Designer.cs
create mode 100644 VisualComponentsForm/VisualComponentsForm/FormWord.cs
create mode 100644 VisualComponentsForm/VisualComponentsForm/FormWord.resx
create mode 100644 VisualComponentsLib/Components/SupportClasses/SimpleTable.cs
diff --git a/VisualComponentsForm/VisualComponentsForm/FormWord.Designer.cs b/VisualComponentsForm/VisualComponentsForm/FormWord.Designer.cs
new file mode 100644
index 0000000..a463221
--- /dev/null
+++ b/VisualComponentsForm/VisualComponentsForm/FormWord.Designer.cs
@@ -0,0 +1,76 @@
+namespace VisualComponentsForm
+{
+ partial class FormWord
+ {
+ ///
+ /// Required designer variable.
+ ///
+ private System.ComponentModel.IContainer components = null;
+
+ ///
+ /// Clean up any resources being used.
+ ///
+ /// true if managed resources should be disposed; otherwise, false.
+ protected override void Dispose(bool disposing)
+ {
+ if (disposing && (components != null))
+ {
+ components.Dispose();
+ }
+ base.Dispose(disposing);
+ }
+
+ #region Windows Form Designer generated code
+
+ ///
+ /// Required method for Designer support - do not modify
+ /// the contents of this method with the code editor.
+ ///
+ private void InitializeComponent()
+ {
+ components = new System.ComponentModel.Container();
+ componentWord = new VisualComponentsLib.Components.ComponentWord(components);
+ buttonFirstTask = new Button();
+ groupBox1 = new GroupBox();
+ groupBox1.SuspendLayout();
+ SuspendLayout();
+ //
+ // buttonFirstTask
+ //
+ buttonFirstTask.Location = new Point(55, 40);
+ buttonFirstTask.Name = "buttonFirstTask";
+ buttonFirstTask.Size = new Size(75, 23);
+ buttonFirstTask.TabIndex = 0;
+ buttonFirstTask.Text = "Создать";
+ buttonFirstTask.UseVisualStyleBackColor = true;
+ buttonFirstTask.Click += ButtonFirstTask_Click;
+ //
+ // groupBox1
+ //
+ groupBox1.Controls.Add(buttonFirstTask);
+ groupBox1.Location = new Point(45, 12);
+ groupBox1.Name = "groupBox1";
+ groupBox1.Size = new Size(200, 100);
+ groupBox1.TabIndex = 1;
+ groupBox1.TabStop = false;
+ groupBox1.Text = "Простая таблица";
+ //
+ // FormWord
+ //
+ AutoScaleDimensions = new SizeF(7F, 15F);
+ AutoScaleMode = AutoScaleMode.Font;
+ ClientSize = new Size(295, 123);
+ Controls.Add(groupBox1);
+ Name = "FormWord";
+ Text = "Невизуальные компоненты";
+ groupBox1.ResumeLayout(false);
+ ResumeLayout(false);
+ }
+
+ #endregion
+
+ private VisualComponentsLib.Components.ComponentWord componentWord;
+ private Button buttonFirstTask;
+ private GroupBox groupBox1;
+ }
+}
\ No newline at end of file
diff --git a/VisualComponentsForm/VisualComponentsForm/FormWord.cs b/VisualComponentsForm/VisualComponentsForm/FormWord.cs
new file mode 100644
index 0000000..a0f00b9
--- /dev/null
+++ b/VisualComponentsForm/VisualComponentsForm/FormWord.cs
@@ -0,0 +1,61 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Data;
+using System.Drawing;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Forms;
+using VisualComponentsLib.Components.SupportClasses;
+using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
+
+namespace VisualComponentsForm
+{
+ public partial class FormWord : Form
+ {
+ SimpleTable simpleTable;
+
+ string[,] first = {
+ { "Первый", "Второй" }
+ };
+
+ string[,] second = {
+ { "Третий", "Четвёртый" }
+ };
+
+ string[,] third = {
+ { "Пятый", "Шестой" }
+ };
+
+ public FormWord()
+ {
+ InitializeComponent();
+ }
+
+ private void ButtonFirstTask_Click(object sender, EventArgs e)
+ {
+ //фильтрация файлов для диалогового окна
+ using var dialog = new SaveFileDialog
+ {
+ Filter = "docx|*.docx"
+ };
+
+ if (dialog.ShowDialog() == DialogResult.OK)
+ {
+ try
+ {
+ simpleTable = new(dialog.FileName, "Первое задание", new List { first, second, third });
+
+ componentWord.CreateDoc(simpleTable);
+
+ MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ }
+ }
+ }
+ }
+}
diff --git a/VisualComponentsForm/VisualComponentsForm/FormWord.resx b/VisualComponentsForm/VisualComponentsForm/FormWord.resx
new file mode 100644
index 0000000..d376b3a
--- /dev/null
+++ b/VisualComponentsForm/VisualComponentsForm/FormWord.resx
@@ -0,0 +1,123 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ text/microsoft-resx
+
+
+ 2.0
+
+
+ System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
+
+
+ 17, 17
+
+
\ No newline at end of file
diff --git a/VisualComponentsForm/VisualComponentsForm/Program.cs b/VisualComponentsForm/VisualComponentsForm/Program.cs
index 9647fa6..865a38b 100644
--- a/VisualComponentsForm/VisualComponentsForm/Program.cs
+++ b/VisualComponentsForm/VisualComponentsForm/Program.cs
@@ -11,7 +11,7 @@ namespace VisualComponentsForm
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
- Application.Run(new FormMain());
+ Application.Run(new FormWord());
}
}
}
\ No newline at end of file
diff --git a/VisualComponentsLib/Components/ComponentWord.cs b/VisualComponentsLib/Components/ComponentWord.cs
index 13df31c..74761d2 100644
--- a/VisualComponentsLib/Components/ComponentWord.cs
+++ b/VisualComponentsLib/Components/ComponentWord.cs
@@ -1,15 +1,24 @@
-using System;
+using DocumentFormat.OpenXml.Packaging;
+using DocumentFormat.OpenXml.Wordprocessing;
+using DocumentFormat.OpenXml;
+using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
+using VisualComponentsLib.Components.SupportClasses;
+using DocumentFormat.OpenXml.EMMA;
namespace VisualComponentsLib.Components
{
public partial class ComponentWord : Component
{
+ private WordprocessingDocument? _wordDocument;
+
+ private Body? _docBody;
+
public ComponentWord()
{
InitializeComponent();
@@ -21,5 +30,87 @@ namespace VisualComponentsLib.Components
InitializeComponent();
}
+
+ public void CreateDoc(SimpleTable simpleTable)
+ {
+ //создаём документ word
+ _wordDocument = WordprocessingDocument.Create(simpleTable.FilePath, WordprocessingDocumentType.Document);
+
+ //вытаскиваем главную часть из вордовского документа
+ MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart();
+
+ mainPart.Document = new Document();
+
+ //генерируем тело основной части документа
+ _docBody = mainPart.Document.AppendChild(new Body());
+
+ _wordDocument.Close();
+
+ AddTable(simpleTable);
+ }
+
+ //создание таблицы
+ private void AddTable(SimpleTable simpleTable)
+ {
+ using (var document = WordprocessingDocument.Open(simpleTable.FilePath, true))
+ {
+ var doc = document.MainDocumentPart.Document;
+
+ Paragraph header = new Paragraph(new Run(new Text(simpleTable.TableName)));
+
+ Table table = new Table();
+
+ TableProperties props = 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
+ }
+ ));
+
+ table.AppendChild(props);
+
+ for (var i = 0; i < simpleTable.DataList.Count; i++)
+ {
+ var tr = new TableRow();
+
+ for (var j = 0; j < simpleTable.DataList[i].Length; j++)
+ {
+ var tc = new TableCell();
+
+ tc.Append(new Paragraph(new Run(new Text(simpleTable.DataList[i][0, j]))));
+
+ tc.Append(new TableCellProperties(
+ new TableCellWidth { Type = TableWidthUnitValues.Auto }));
+
+ tr.Append(tc);
+ }
+
+ table.Append(tr);
+ }
+
+ doc.Body.Append(header);
+
+ doc.Body.Append(table);
+
+ doc.Save();
+ }
+ }
}
}
diff --git a/VisualComponentsLib/Components/SupportClasses/SimpleTable.cs b/VisualComponentsLib/Components/SupportClasses/SimpleTable.cs
new file mode 100644
index 0000000..92eb9c0
--- /dev/null
+++ b/VisualComponentsLib/Components/SupportClasses/SimpleTable.cs
@@ -0,0 +1,24 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace VisualComponentsLib.Components.SupportClasses
+{
+ public class SimpleTable
+ {
+ public string FilePath = string.Empty;
+
+ public string TableName = string.Empty;
+
+ public List DataList = new();
+
+ public SimpleTable(string filePath, string tableName, List dataList)
+ {
+ FilePath = filePath;
+ TableName = tableName;
+ DataList = dataList;
+ }
+ }
+}