using Aspose.Words.Drawing.Charts; using Aspose.Words; using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; using Aspose.Words.Tables; using static COPWinForms.ComponentWord2; namespace COPWinForms { public partial class ComponentWord2 : Component { public ComponentWord2() { InitializeComponent(); } public ComponentWord2(IContainer container) { container.Add(this); InitializeComponent(); } public void CreateTable(TableWord tableWord) { // Проверка наличия данных и определений столбцов if (tableWord.Data == null) { throw new ArgumentException("Data or column definitions are null or empty"); } // Проверка, что все ячейки шапки заполнены и для каждого столбца определено свойство/поле класса foreach (var columnDefinition in tableWord.ColumnDefinitions) { if (string.IsNullOrEmpty(columnDefinition.Header) || string.IsNullOrEmpty(columnDefinition.PropertyName)) { throw new ArgumentException($"Incomplete column definition: {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(tableWord.DocumentTitle); // Создание таблицы Table table = builder.StartTable(); // Вставка первой строки шапки таблицы foreach (var columnDefinition in tableWord.ColumnDefinitions) { builder.InsertCell(); builder.Write(columnDefinition.Header); } // Объединение ячеек в шапке таблицы foreach (var mergedColumn in tableWord.MergedColumns) { 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 columnDefinition2 in tableWord.ColumnDefinitions2) { builder.InsertCell(); // Получение значения свойства/поля объекта по заданному имени var propertyValue = item.GetType() .GetProperty(columnDefinition2.PropertyName)? .GetValue(item)?.ToString(); // Вставка значения в ячейку builder.Write(propertyValue ?? ""); } builder.EndRow(); } builder.EndTable(); // Сохранение документа в файл document.Save(tableWord.FilePath); } public class ColumnDefinition { public string Header { get; set; } public string PropertyName { get; set; } } public class ColumnDefinition2 { public string Header { get; set; } public string PropertyName { get; set; } } } }