2023-10-12 00:24:32 +04:00
|
|
|
|
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;
|
2023-10-13 20:00:14 +04:00
|
|
|
|
using static COPWinForms.ComponentWord2;
|
2023-10-12 00:24:32 +04:00
|
|
|
|
|
|
|
|
|
namespace COPWinForms
|
|
|
|
|
{
|
|
|
|
|
public partial class ComponentWord2 : Component
|
|
|
|
|
{
|
|
|
|
|
public ComponentWord2()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ComponentWord2(IContainer container)
|
|
|
|
|
{
|
|
|
|
|
container.Add(this);
|
|
|
|
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-12 22:42:25 +04:00
|
|
|
|
public void CreateTable(TableWord tableWord)
|
2023-10-12 00:24:32 +04:00
|
|
|
|
{
|
|
|
|
|
// Проверка наличия данных и определений столбцов
|
2023-10-12 22:42:25 +04:00
|
|
|
|
if (tableWord.Data == null)
|
2023-10-12 00:24:32 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Data or column definitions are null or empty");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Проверка, что все ячейки шапки заполнены и для каждого столбца определено свойство/поле класса
|
2023-10-12 22:42:25 +04:00
|
|
|
|
foreach (var columnDefinition in tableWord.ColumnDefinitions)
|
2023-10-12 00:24:32 +04:00
|
|
|
|
{
|
|
|
|
|
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;
|
2023-10-12 22:42:25 +04:00
|
|
|
|
builder.Writeln(tableWord.DocumentTitle);
|
2023-10-12 00:24:32 +04:00
|
|
|
|
|
|
|
|
|
// Создание таблицы
|
|
|
|
|
Table table = builder.StartTable();
|
|
|
|
|
|
2023-10-12 22:42:25 +04:00
|
|
|
|
// Вставка первой строки шапки таблицы
|
|
|
|
|
foreach (var columnDefinition in tableWord.ColumnDefinitions)
|
|
|
|
|
{
|
|
|
|
|
builder.InsertCell();
|
|
|
|
|
builder.Write(columnDefinition.Header);
|
|
|
|
|
}
|
2023-10-13 20:00:14 +04:00
|
|
|
|
|
|
|
|
|
// Объединение ячеек в шапке таблицы
|
|
|
|
|
foreach (var mergedColumn in tableWord.MergedColumns)
|
|
|
|
|
{
|
|
|
|
|
int startCellIndex = mergedColumn[0];
|
|
|
|
|
int endCellIndex = mergedColumn[mergedColumn.Length - 1];
|
2023-10-12 22:42:25 +04:00
|
|
|
|
|
2023-10-13 20:00:14 +04:00
|
|
|
|
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)
|
2023-10-12 00:24:32 +04:00
|
|
|
|
{
|
|
|
|
|
builder.InsertCell();
|
2023-10-13 20:00:14 +04:00
|
|
|
|
builder.Write(columnDefinition2.Header);
|
2023-10-12 00:24:32 +04:00
|
|
|
|
}
|
|
|
|
|
builder.EndRow();
|
|
|
|
|
// Вставка данных в таблицу
|
2023-10-12 22:42:25 +04:00
|
|
|
|
foreach (var item in tableWord.Data)
|
2023-10-12 00:24:32 +04:00
|
|
|
|
{
|
2023-10-13 20:00:14 +04:00
|
|
|
|
foreach (var columnDefinition2 in tableWord.ColumnDefinitions2)
|
2023-10-12 00:24:32 +04:00
|
|
|
|
{
|
|
|
|
|
builder.InsertCell();
|
|
|
|
|
// Получение значения свойства/поля объекта по заданному имени
|
|
|
|
|
var propertyValue = item.GetType()
|
2023-10-13 20:00:14 +04:00
|
|
|
|
.GetProperty(columnDefinition2.PropertyName)?
|
|
|
|
|
.GetValue(item)?.ToString();
|
2023-10-12 00:24:32 +04:00
|
|
|
|
|
|
|
|
|
// Вставка значения в ячейку
|
|
|
|
|
builder.Write(propertyValue ?? "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.EndRow();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-13 20:00:14 +04:00
|
|
|
|
builder.EndTable();
|
2023-10-12 00:24:32 +04:00
|
|
|
|
|
|
|
|
|
// Сохранение документа в файл
|
2023-10-12 22:42:25 +04:00
|
|
|
|
document.Save(tableWord.FilePath);
|
2023-10-12 00:24:32 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ColumnDefinition
|
|
|
|
|
{
|
|
|
|
|
public string Header { get; set; }
|
|
|
|
|
public string PropertyName { get; set; }
|
|
|
|
|
}
|
2023-10-13 20:00:14 +04:00
|
|
|
|
|
|
|
|
|
public class ColumnDefinition2
|
|
|
|
|
{
|
|
|
|
|
public string Header { get; set; }
|
|
|
|
|
public string PropertyName { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-10-12 00:24:32 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|