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
|
|
|
|
{
|
2023-10-13 22:48:40 +04:00
|
|
|
|
if (string.IsNullOrEmpty(columnDefinition.PropertyName))
|
2023-10-12 00:24:32 +04:00
|
|
|
|
{
|
|
|
|
|
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-13 22:48:40 +04:00
|
|
|
|
|
2023-10-12 22:42:25 +04:00
|
|
|
|
// Вставка первой строки шапки таблицы
|
|
|
|
|
foreach (var columnDefinition in tableWord.ColumnDefinitions)
|
|
|
|
|
{
|
2023-10-13 22:48:40 +04:00
|
|
|
|
builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(40);
|
2023-10-12 22:42:25 +04:00
|
|
|
|
builder.InsertCell();
|
|
|
|
|
builder.Write(columnDefinition.Header);
|
|
|
|
|
}
|
2023-10-13 22:48:40 +04:00
|
|
|
|
// Объединение ячеек в первой строке шапки таблицы
|
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++)
|
|
|
|
|
{
|
2023-10-13 22:48:40 +04:00
|
|
|
|
table.Rows[0].Cells[i].CellFormat.HorizontalMerge = CellMerge.First;
|
|
|
|
|
table.Rows[0].Cells[i].CellFormat.VerticalMerge = CellMerge.First;
|
2023-10-13 20:00:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = startCellIndex + 1; i <= endCellIndex; i++)
|
|
|
|
|
{
|
2023-10-13 22:48:40 +04:00
|
|
|
|
table.Rows[0].Cells[i].CellFormat.HorizontalMerge = CellMerge.Previous;
|
|
|
|
|
table.Rows[0].Cells[i].CellFormat.VerticalMerge = CellMerge.First;
|
2023-10-13 20:00:14 +04:00
|
|
|
|
}
|
2023-10-13 22:48:40 +04:00
|
|
|
|
|
|
|
|
|
|
2023-10-13 20:00:14 +04:00
|
|
|
|
}
|
2023-10-13 22:48:40 +04:00
|
|
|
|
|
2023-10-13 20:00:14 +04:00
|
|
|
|
builder.EndRow();
|
2023-10-13 22:48:40 +04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ColInd = 0;
|
2023-10-13 20:00:14 +04:00
|
|
|
|
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-13 22:48:40 +04:00
|
|
|
|
builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(tableWord.WeightColumn[ColInd]);
|
|
|
|
|
ColInd++;
|
2023-10-12 00:24:32 +04:00
|
|
|
|
}
|
2023-10-13 22:48:40 +04:00
|
|
|
|
|
2023-10-12 00:24:32 +04:00
|
|
|
|
builder.EndRow();
|
2023-10-13 22:48:40 +04:00
|
|
|
|
|
|
|
|
|
int columnIndex;
|
|
|
|
|
foreach (var columnDefinition in tableWord.ColumnDefinitions)
|
|
|
|
|
{
|
|
|
|
|
string currentPropertyName = columnDefinition.PropertyName;
|
|
|
|
|
columnIndex = 0;
|
|
|
|
|
foreach (var columnDefinition2 in tableWord.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++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2023-10-12 00:24:32 +04:00
|
|
|
|
// Вставка данных в таблицу
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|