using System.ComponentModel; using System.Linq; using Aspose.Words; using Aspose.Words.Tables; using COP_1.cop_2.helpers; namespace COP_1.cop_2 { public partial class WordTable : Component { public WordTable() { InitializeComponent(); } public WordTable(IContainer container) { container.Add(this); InitializeComponent(); } public void CreateTable(WordTableInfo tableWord) where T : class { // Проверка наличия данных и определений столбцов if (tableWord.Items == null || tableWord.Items.Count == 0 || tableWord.ColumnParameters == null || tableWord.ColumnParameters.Count == 0) { throw new ArgumentException("Data or column definitions are null or empty"); } // Проверка, что все ячейки шапки заполнены и для каждого столбца определено свойство/поле класса foreach (var columnDefinition in tableWord.ColumnParameters) { if (string.IsNullOrEmpty(columnDefinition.PropertyName)) { throw new ArgumentException($"Incomplete column definition: {columnDefinition.FirstRowHeader}"); } } // Создание документа 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.Title); // Создание таблицы Table table = builder.StartTable(); // Создание первой строки (заголовок) foreach (var columnDefinition in tableWord.ColumnParameters) { builder.InsertCell(); builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(columnDefinition.Width); builder.Write(columnDefinition.FirstRowHeader); } // Создание второй строки (вторые заголовки) builder.EndRow(); // Завершение первой строки заголовка foreach (var columnDefinition in tableWord.ColumnParameters) { builder.InsertCell(); builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(columnDefinition.Width); builder.Write(columnDefinition.SecondRowHeader); } builder.EndRow(); int startCellIndex = -1; int endCellIndex = -1; // Объединение ячеек в первой строке шапки таблицы (если необходимо) foreach (var mergedColumn in tableWord.MergedColumns) { startCellIndex = mergedColumn[0]; endCellIndex = mergedColumn[^1]; for (int i = startCellIndex; i <= endCellIndex; i++) { table.Rows[0].Cells[i].CellFormat.HorizontalMerge = i == startCellIndex ? CellMerge.First : CellMerge.Previous; } } // Установка вертикального объединения заголовков for (int columnIndex = 0; columnIndex < tableWord.ColumnParameters.Count; columnIndex++) { if (startCellIndex == columnIndex || endCellIndex == columnIndex) continue; table.Rows[0].Cells[columnIndex].CellFormat.VerticalMerge = CellMerge.First; table.Rows[1].Cells[columnIndex].CellFormat.VerticalMerge = CellMerge.Previous; } // Вставка данных в таблицу foreach (var item in tableWord.Items) { foreach (var columnDefinition in tableWord.ColumnParameters) { builder.InsertCell(); // Получение значения свойства/поля объекта по заданному имени var propertyValue = item.GetType().GetProperty(columnDefinition.PropertyName)?.GetValue(item)?.ToString(); builder.Write(propertyValue ?? string.Empty); } builder.EndRow(); } builder.EndTable(); // Сохранение документа в файл document.Save(tableWord.FilePath); } } }