COP-17/WinFormsProject/WinFormsLibrary/Table2column.cs

135 lines
4.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
using Aspose.Words.Tables;
using WinFormsLibrary.SupportClasses;
namespace WinFormsLibrary
{
public partial class Table2column : Component
{
public Table2column()
{
InitializeComponent();
}
public Table2column(IContainer container)
{
container.Add(this);
InitializeComponent();
}
public void CreateTable<T>(BigTable<T> bigTable)
{
if (bigTable.Data == null)
{
throw new ArgumentException("Не заданы все данные");
}
foreach (var columnDefinition in bigTable.ColumnDefinitions)
{
if (string.IsNullOrEmpty(columnDefinition.PropertyName))
{
throw new ArgumentException($"Не задано свойство столбца: {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(bigTable.DocumentTitle);
Table table = builder.StartTable();
foreach (var columnDefinition in bigTable.ColumnDefinitions)
{
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(columnDefinition.Weight);
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.Write(columnDefinition.Header);
}
foreach (var mergedColumn in bigTable.MergedColumns)
{
int startCellIndex = mergedColumn[0];
int endCellIndex = mergedColumn[mergedColumn.Length - 1];
for (int i = startCellIndex; i <= endCellIndex; i++)
{
table.Rows[0].Cells[i].CellFormat.HorizontalMerge = CellMerge.First;
table.Rows[0].Cells[i].CellFormat.VerticalMerge = CellMerge.First;
}
for (int i = startCellIndex + 1; i <= endCellIndex; i++)
{
table.Rows[0].Cells[i].CellFormat.HorizontalMerge = CellMerge.Previous;
table.Rows[0].Cells[i].CellFormat.VerticalMerge = CellMerge.First;
}
}
builder.EndRow();
foreach (var columnDefinition2 in bigTable.ColumnDefinitions2)
{
builder.InsertCell();
builder.CellFormat.PreferredWidth = PreferredWidth.FromPoints(columnDefinition2.Weight);
builder.Write(columnDefinition2.Header);
}
builder.EndRow();
int columnIndex;
foreach (var columnDefinition in bigTable.ColumnDefinitions)
{
string currentPropertyName = columnDefinition.PropertyName;
columnIndex = 0;
foreach (var columnDefinition2 in bigTable.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++;
}
}
foreach (var item in bigTable.Data)
{
foreach (var columnDefinition2 in bigTable.ColumnDefinitions2)
{
builder.InsertCell();
var propertyValue = item.GetType()
.GetProperty(columnDefinition2.PropertyName)?
.GetValue(item)?.ToString();
builder.Write(propertyValue ?? "");
}
builder.EndRow();
}
builder.EndTable();
document.Save(bigTable.FilePath);
}
}
}