96 lines
2.8 KiB
C#
96 lines
2.8 KiB
C#
using ShabComponentsLibrary.OfficePackage;
|
||
using ShabComponentsLibrary.OfficePackage.HelperEnums;
|
||
using ShabComponentsLibrary.OfficePackage.HelperModels;
|
||
using System.ComponentModel;
|
||
|
||
namespace ShabComponentsLibrary
|
||
{
|
||
/// <summary>
|
||
/// Невизуальный компонент для создания документа с таблицей,
|
||
/// у которой шапкой является первая строка и первый столбец
|
||
/// </summary>
|
||
public partial class ShabConfigurableTableComponent : Component
|
||
{
|
||
public ShabConfigurableTableComponent()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
public ShabConfigurableTableComponent(IContainer Container)
|
||
{
|
||
Container.Add(this);
|
||
InitializeComponent();
|
||
}
|
||
|
||
public void CreateDocument<T>(string Filename, string Title,
|
||
List<(double ColumnWidth, string TableHeader, string HeaderPropertyName)> ColumnData,
|
||
List<double> RowHeights, List<T> Data)
|
||
{
|
||
if (string.IsNullOrEmpty(Filename))
|
||
throw new ArgumentException("Filename cannot be empty");
|
||
if (string.IsNullOrEmpty(Title))
|
||
throw new ArgumentException("Title cannot be empty");
|
||
if (ColumnData.Count == 0)
|
||
throw new ArgumentException("Headers cannot be empty");
|
||
if (Data.Count == 0)
|
||
throw new ArgumentException("Data cannot be empty");
|
||
if (RowHeights.Count != 2)
|
||
throw new ArgumentException("Row heights should be specified for header row and other rows");
|
||
|
||
SaveToPdf Pdf = new SaveToPdf();
|
||
Pdf.CreatePdf();
|
||
Pdf.CreateParagraph(new PdfParagraph
|
||
{
|
||
Text = Title,
|
||
Style = "NormalTitle",
|
||
ParagraphAlignment = PdfParagraphAlignmentType.Left
|
||
});
|
||
|
||
Pdf.CreateTable(ColumnData.Select(x => x.ColumnWidth).ToList());
|
||
|
||
// Шапка
|
||
Pdf.CreateRow(new PdfRowParameters
|
||
{
|
||
Texts = ColumnData.Select(x => x.TableHeader).ToList(),
|
||
Style = "NormalTitle",
|
||
ParagraphAlignment = PdfParagraphAlignmentType.Center,
|
||
RowHeight = RowHeights[0]
|
||
});
|
||
|
||
// Данные
|
||
foreach (var Object in Data)
|
||
{
|
||
List<string> Cells = new List<string>();
|
||
|
||
for (int ColumnIndex = 0; ColumnIndex < ColumnData.Count; ++ColumnIndex)
|
||
{
|
||
string PropertyName = ColumnData[ColumnIndex].HeaderPropertyName;
|
||
var Property = typeof(T).GetProperty(PropertyName);
|
||
if (Property is null)
|
||
{
|
||
throw new ArgumentException($"Failed to find property {PropertyName} in provided objects class");
|
||
}
|
||
|
||
var PropertyValue = Property.GetValue(Object)?.ToString();
|
||
if (PropertyValue is not null)
|
||
{
|
||
Cells.Add(PropertyValue);
|
||
}
|
||
}
|
||
|
||
Pdf.CreateRow(new PdfRowParameters
|
||
{
|
||
Texts = Cells,
|
||
Style = "Normal",
|
||
FirstCellStyle = "NormalTitle",
|
||
ParagraphAlignment = PdfParagraphAlignmentType.Left,
|
||
FirstCellParagraphAlignment = PdfParagraphAlignmentType.Center,
|
||
RowHeight = RowHeights[1]
|
||
});
|
||
}
|
||
|
||
Pdf.SavePdf(Filename);
|
||
}
|
||
}
|
||
}
|