From 8accba9c6f1e97d658004db8030830da8aadb1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BA=D0=B0=D1=82=D0=B5=D1=80=D0=B8=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=A0=D0=BE=D0=B3=D0=B0=D1=88=D0=BE=D0=B2=D0=B0?= Date: Fri, 13 Oct 2023 20:00:14 +0400 Subject: [PATCH] =?UTF-8?q?=D1=82=D1=83=D0=BF=D0=BE=D0=B5=202=20=D0=B7?= =?UTF-8?q?=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- COPWinForms/CellMergeInfo.cs | 16 ++++++++++++ COPWinForms/ComponentWord2.cs | 47 +++++++++++++++++++++++++---------- COPWinForms/TableWord.cs | 7 ++++-- WinFormsTest/FormTestApp.cs | 18 +++++++++++--- 4 files changed, 69 insertions(+), 19 deletions(-) create mode 100644 COPWinForms/CellMergeInfo.cs diff --git a/COPWinForms/CellMergeInfo.cs b/COPWinForms/CellMergeInfo.cs new file mode 100644 index 0000000..281337e --- /dev/null +++ b/COPWinForms/CellMergeInfo.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace COPWinForms +{ + public class CellMergeInfo + { + public int StartColumnIndex { get; set; } + public int EndColumnIndex { get; set; } + public int RowIndex { get; set; } + public string PropertyName { get; set; } + } +} diff --git a/COPWinForms/ComponentWord2.cs b/COPWinForms/ComponentWord2.cs index 9cd1f94..834527b 100644 --- a/COPWinForms/ComponentWord2.cs +++ b/COPWinForms/ComponentWord2.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using Aspose.Words.Tables; +using static COPWinForms.ComponentWord2; namespace COPWinForms { @@ -63,37 +64,49 @@ namespace COPWinForms builder.InsertCell(); builder.Write(columnDefinition.Header); } - builder.EndRow(); - - // Вставка второй строки шапки таблицы - foreach (var columnDefinition in tableWord.ColumnDefinitions) + + // Объединение ячеек в шапке таблицы + foreach (var mergedColumn in tableWord.MergedColumns) { - builder.InsertCell(); - builder.Write(columnDefinition.Header); + int startCellIndex = mergedColumn[0]; + int endCellIndex = mergedColumn[mergedColumn.Length - 1]; + + for (int i = startCellIndex; i <= endCellIndex; i++) + { + table.FirstRow.Cells[i].CellFormat.HorizontalMerge = CellMerge.First; + } + + for (int i = startCellIndex + 1; i <= endCellIndex; i++) + { + builder.CellFormat.HorizontalMerge = CellMerge.Previous; + } + } + builder.EndRow(); + foreach (var columnDefinition2 in tableWord.ColumnDefinitions2) + { + builder.InsertCell(); + builder.Write(columnDefinition2.Header); } builder.EndRow(); - // Вставка данных в таблицу foreach (var item in tableWord.Data) { - - foreach (var columnDefinition in tableWord.ColumnDefinitions) + foreach (var columnDefinition2 in tableWord.ColumnDefinitions2) { builder.InsertCell(); // Получение значения свойства/поля объекта по заданному имени var propertyValue = item.GetType() - .GetProperty(columnDefinition.PropertyName)? - .GetValue(item)?.ToString(); + .GetProperty(columnDefinition2.PropertyName)? + .GetValue(item)?.ToString(); // Вставка значения в ячейку builder.Write(propertyValue ?? ""); - } builder.EndRow(); } - builder.EndTable(); + builder.EndTable(); // Сохранение документа в файл document.Save(tableWord.FilePath); @@ -104,6 +117,14 @@ namespace COPWinForms public string Header { get; set; } public string PropertyName { get; set; } } + + public class ColumnDefinition2 + { + public string Header { get; set; } + public string PropertyName { get; set; } + } + + } } diff --git a/COPWinForms/TableWord.cs b/COPWinForms/TableWord.cs index 1c7e04d..18e3013 100644 --- a/COPWinForms/TableWord.cs +++ b/COPWinForms/TableWord.cs @@ -14,16 +14,19 @@ namespace COPWinForms public string DocumentTitle = string.Empty; public List ColumnDefinitions; + public List ColumnDefinitions2; public List Data; - - public TableWord(string filePath, string documentTitle, List columnDefinitions, List data) + public List MergedColumns; + public TableWord(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/WinFormsTest/FormTestApp.cs b/WinFormsTest/FormTestApp.cs index 5a720a6..44e1764 100644 --- a/WinFormsTest/FormTestApp.cs +++ b/WinFormsTest/FormTestApp.cs @@ -159,14 +159,24 @@ namespace WinFormsTest private void button3_Click(object sender, EventArgs e) { - ComponentWord2 tableComponent = new ComponentWord2(); + List mergedColumns = new() + { + new int[] { 0, 1 } // 0 1 + }; + List columnDefinitions = new List { - new ColumnDefinition { Header = "FIO", PropertyName = "FIO" }, - new ColumnDefinition { Header = "Group", PropertyName = "Group" }, + new ColumnDefinition { Header = " ", PropertyName = "PersonalData" }, new ColumnDefinition { Header = "Email", PropertyName = "Email" } }; + List columnDefinitions2 = new List + { + new ColumnDefinition2 { Header = "FIO", PropertyName = "FIO" }, + new ColumnDefinition2 { Header = "Group", PropertyName = "Group" }, + new ColumnDefinition2 { Header = "Email", PropertyName = "Email" } + }; + List data = new List { new Student { FIO = "John", Group = "ff", Email = "New York" }, @@ -183,7 +193,7 @@ namespace WinFormsTest { try { - TableWord tableWord = new(dialog.FileName, " 2", columnDefinitions, data); + TableWord tableWord = new(dialog.FileName, " 2", columnDefinitions, columnDefinitions2, data, mergedColumns); componentWord21.CreateTable(tableWord); MessageBox.Show(" ", "", MessageBoxButtons.OK, MessageBoxIcon.Information); }