diff --git a/COP/VisualCompLib/Components/SupportClasses/BigTable.cs b/COP/VisualCompLib/Components/SupportClasses/BigTable.cs new file mode 100644 index 0000000..6842239 --- /dev/null +++ b/COP/VisualCompLib/Components/SupportClasses/BigTable.cs @@ -0,0 +1,25 @@ +namespace VisualCompLib.Components.SupportClasses +{ + public class BigTable + { + public string FilePath = string.Empty; + + public string DocumentTitle = string.Empty; + + public List ColumnDefinitions; + public List ColumnDefinitions2; + + public List Data; + + public List MergedColumns; + public BigTable(string filePath, string documentTitle, List columnDefinitions, List columnDefinitions2, List data, List mergedColumns) + { + FilePath = filePath; + DocumentTitle = documentTitle; + ColumnDefinitions = columnDefinitions; + Data = data; + MergedColumns = mergedColumns; + ColumnDefinitions2 = columnDefinitions2; + } + } +} diff --git a/COP/VisualCompLib/Components/SupportClasses/ColumnDefenition.cs b/COP/VisualCompLib/Components/SupportClasses/ColumnDefenition.cs new file mode 100644 index 0000000..4ceddce --- /dev/null +++ b/COP/VisualCompLib/Components/SupportClasses/ColumnDefenition.cs @@ -0,0 +1,9 @@ +namespace VisualCompLib.Components.SupportClasses +{ + public class ColumnDefinition + { + public string Header; + public string PropertyName; + public double Weight; + } +} diff --git a/COP/VisualCompLib/Components/WordTable.Designer.cs b/COP/VisualCompLib/Components/WordTable.Designer.cs new file mode 100644 index 0000000..6561641 --- /dev/null +++ b/COP/VisualCompLib/Components/WordTable.Designer.cs @@ -0,0 +1,36 @@ +namespace VisualCompLib.Components +{ + partial class WordTable + { + /// + /// Обязательная переменная конструктора. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Освободить все используемые ресурсы. + /// + /// истинно, если управляемый ресурс должен быть удален; иначе ложно. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Код, автоматически созданный конструктором компонентов + + /// + /// Требуемый метод для поддержки конструктора — не изменяйте + /// содержимое этого метода с помощью редактора кода. + /// + private void InitializeComponent() + { + components = new System.ComponentModel.Container(); + } + + #endregion + } +} diff --git a/COP/VisualCompLib/Components/WordTable.cs b/COP/VisualCompLib/Components/WordTable.cs new file mode 100644 index 0000000..2dab936 --- /dev/null +++ b/COP/VisualCompLib/Components/WordTable.cs @@ -0,0 +1,128 @@ +using System.ComponentModel; +using VisualCompLib.Components.SupportClasses; +using Aspose.Words; +using Aspose.Words.Tables; + +namespace VisualCompLib.Components +{ + public partial class WordTable : Component + { + public WordTable() + { + InitializeComponent(); + } + + public WordTable(IContainer container) + { + container.Add(this); + + InitializeComponent(); + } + + public void CreateTable(BigTable bigTable) + { + if (bigTable.Data == null) + { + throw new ArgumentException("Не заданы все данные"); + } + + foreach (var columnDefinition in bigTable.ColumnDefinitions) + { + if (string.IsNullOrEmpty(columnDefinition.PropertyName)) + { + throw new ArgumentException($"Не задано свойство столбца: {columnDefinition.Header}"); + } + } + + Document document = new Document(); + DocumentBuilder builder = new DocumentBuilder(document); + + Style titleStyle = builder.Document.Styles.Add(StyleType.Paragraph, "Title"); + titleStyle.Font.Size = 16; + titleStyle.Font.Bold = true; + + builder.ParagraphFormat.Style = titleStyle; + builder.Writeln(bigTable.DocumentTitle); + + Table table = builder.StartTable(); + + + foreach (var columnDefinition in bigTable.ColumnDefinitions) + { + builder.InsertCell(); + builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(columnDefinition.Weight); + builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; + builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; + builder.Write(columnDefinition.Header); + } + + foreach (var mergedColumn in bigTable.MergedColumns) + { + int startCellIndex = mergedColumn[0]; + int endCellIndex = mergedColumn[mergedColumn.Length - 1]; + + for (int i = startCellIndex; i <= endCellIndex; i++) + { + table.Rows[0].Cells[i].CellFormat.HorizontalMerge = CellMerge.First; + table.Rows[0].Cells[i].CellFormat.VerticalMerge = CellMerge.First; + } + + for (int i = startCellIndex + 1; i <= endCellIndex; i++) + { + table.Rows[0].Cells[i].CellFormat.HorizontalMerge = CellMerge.Previous; + table.Rows[0].Cells[i].CellFormat.VerticalMerge = CellMerge.First; + } + + + } + builder.EndRow(); + + foreach (var columnDefinition2 in bigTable.ColumnDefinitions2) + { + builder.InsertCell(); + builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(columnDefinition2.Weight); + builder.Write(columnDefinition2.Header); + } + + builder.EndRow(); + + int columnIndex; + foreach (var columnDefinition in bigTable.ColumnDefinitions) + { + string currentPropertyName = columnDefinition.PropertyName; + columnIndex = 0; + foreach (var columnDefinition2 in bigTable.ColumnDefinitions2) + { + string currentPropertyName1 = columnDefinition2.PropertyName; + + if (currentPropertyName == currentPropertyName1) + { + table.Rows[0].Cells[columnIndex].CellFormat.VerticalMerge = CellMerge.First; + table.Rows[1].Cells[columnIndex].CellFormat.VerticalMerge = CellMerge.Previous; + + } + columnIndex++; + } + } + + foreach (var item in bigTable.Data) + { + foreach (var columnDefinition2 in bigTable.ColumnDefinitions2) + { + builder.InsertCell(); + var propertyValue = item.GetType() + .GetProperty(columnDefinition2.PropertyName)? + .GetValue(item)?.ToString(); + + builder.Write(propertyValue ?? ""); + } + + builder.EndRow(); + } + + builder.EndTable(); + + document.Save(bigTable.FilePath); + } + } +} diff --git a/COP/WinForms/FormWord.Designer.cs b/COP/WinForms/FormWord.Designer.cs index 726bb77..17af3bb 100644 --- a/COP/WinForms/FormWord.Designer.cs +++ b/COP/WinForms/FormWord.Designer.cs @@ -35,8 +35,12 @@ wordLineChart = new VisualCompLib.Components.WordLineChart(components); groupBox3 = new GroupBox(); button3 = new Button(); + groupBox2 = new GroupBox(); + button2 = new Button(); + wordTable = new VisualCompLib.Components.WordTable(components); groupBox1.SuspendLayout(); groupBox3.SuspendLayout(); + groupBox2.SuspendLayout(); SuspendLayout(); // // groupBox1 @@ -62,7 +66,7 @@ // groupBox3 // groupBox3.Controls.Add(button3); - groupBox3.Location = new Point(158, 12); + groupBox3.Location = new Point(331, 12); groupBox3.Name = "groupBox3"; groupBox3.Size = new Size(120, 80); groupBox3.TabIndex = 1; @@ -79,17 +83,39 @@ button3.UseVisualStyleBackColor = true; button3.Click += button3_Click; // + // groupBox2 + // + groupBox2.Controls.Add(button2); + groupBox2.Location = new Point(171, 12); + groupBox2.Name = "groupBox2"; + groupBox2.Size = new Size(120, 80); + groupBox2.TabIndex = 1; + groupBox2.TabStop = false; + groupBox2.Text = "Таблица"; + // + // button2 + // + button2.Location = new Point(6, 45); + button2.Name = "button2"; + button2.Size = new Size(94, 29); + button2.TabIndex = 0; + button2.Text = "Создать"; + button2.UseVisualStyleBackColor = true; + button2.Click += button2_Click; + // // FormWord // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(320, 165); + ClientSize = new Size(463, 114); + Controls.Add(groupBox2); Controls.Add(groupBox3); Controls.Add(groupBox1); Name = "FormWord"; Text = "Невизуальные компоненты"; groupBox1.ResumeLayout(false); groupBox3.ResumeLayout(false); + groupBox2.ResumeLayout(false); ResumeLayout(false); } @@ -101,5 +127,8 @@ private VisualCompLib.Components.WordLineChart wordLineChart; private GroupBox groupBox3; private Button button3; + private GroupBox groupBox2; + private Button button2; + private VisualCompLib.Components.WordTable wordTable; } } \ No newline at end of file diff --git a/COP/WinForms/FormWord.cs b/COP/WinForms/FormWord.cs index 1754efc..0ecb7de 100644 --- a/COP/WinForms/FormWord.cs +++ b/COP/WinForms/FormWord.cs @@ -1,6 +1,8 @@ -using System.Diagnostics.Metrics; +using DocumentFormat.OpenXml.Wordprocessing; +using VisualCompLib.Components; using VisualCompLib.Components.SupportClasses; using VisualCompLib.Components.SupportClasses.Enums; +using VisualCompLib.Object; namespace WinForms { @@ -35,6 +37,57 @@ namespace WinForms } } + private void button2_Click(object sender, EventArgs e) + { + List mergedColumns = new() + { + new int[] { 0, 1, 2 } + }; + + + + List columnDefinitions = new List + { + new ColumnDefinition { Header = "Образование", PropertyName = "Eduction", Weight = 35 }, + new ColumnDefinition { Header = "", PropertyName = "Education1", Weight = 35 }, + new ColumnDefinition { Header = "", PropertyName = "Education2", Weight = 10 }, + new ColumnDefinition { Header = "Фамилия", PropertyName = "Name", Weight = 20 } + }; + + List columnDefinitions2 = new List + { + new ColumnDefinition { Header = "Группа", PropertyName = "Group", Weight = 35 }, + new ColumnDefinition { Header = "Факультатив", PropertyName = "Faculty", Weight = 35 }, + new ColumnDefinition { Header = "Курс", PropertyName = "Course", Weight = 10 }, + new ColumnDefinition { Header = "Фамилия", PropertyName = "Name", Weight = 20 } + }; + + List data = new List + { + new Student { Group = "ПИбд-32", Faculty = "ФИСТ", Course = 3, Name = "Васильев" }, + new Student { Group = "РТбд-11", Faculty = "РТФ", Course = 1, Name = "Иванов" }, + new Student { Group = "ЛМККбд-41", Faculty = "ГФ", Course = 4, Name = "Смирнова" } + }; + + using var dialog = new SaveFileDialog + { + Filter = "docx|*.docx" + }; + if (dialog.ShowDialog() == DialogResult.OK) + { + try + { + BigTable bigTable = new(dialog.FileName, "Задание 2", columnDefinitions, columnDefinitions2, data, mergedColumns); + wordTable.CreateTable(bigTable); + MessageBox.Show("Выполнено", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + private void button3_Click(object sender, EventArgs e) { //фильтрация файлов для диалогового окна diff --git a/COP/WinForms/FormWord.resx b/COP/WinForms/FormWord.resx index 6501934..a545853 100644 --- a/COP/WinForms/FormWord.resx +++ b/COP/WinForms/FormWord.resx @@ -123,4 +123,7 @@ 134, 17 + + 286, 17 + \ No newline at end of file