99 lines
3.0 KiB
C#
99 lines
3.0 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<string> ColumnWidths, List<string> RowHeights,
|
|||
|
List<string> TableHeaders, List<string> HeaderPropertyNames, 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 (TableHeaders.Count == 0)
|
|||
|
throw new ArgumentException("Headers cannot be empty");
|
|||
|
if (Data.Count == 0)
|
|||
|
throw new ArgumentException("Data cannot be empty");
|
|||
|
if (ColumnWidths.Count != TableHeaders.Count)
|
|||
|
throw new ArgumentException("Column widths count must match headers count");
|
|||
|
if (RowHeights.Count != 2)
|
|||
|
throw new ArgumentException("Row heights should be specified for header row and other rows");
|
|||
|
if (HeaderPropertyNames.Count != TableHeaders.Count)
|
|||
|
throw new ArgumentException("Each column must have a corresponding property mapping");
|
|||
|
|
|||
|
SaveToPdf Pdf = new SaveToPdf();
|
|||
|
Pdf.CreatePdf();
|
|||
|
Pdf.CreateParagraph(new PdfParagraph
|
|||
|
{
|
|||
|
Text = Title,
|
|||
|
Style = "NormalTitle",
|
|||
|
ParagraphAlignment = PdfParagraphAlignmentType.Left
|
|||
|
});
|
|||
|
|
|||
|
Pdf.CreateTable(ColumnWidths);
|
|||
|
|
|||
|
// Шапка
|
|||
|
Pdf.CreateRow(new PdfRowParameters
|
|||
|
{
|
|||
|
Texts = TableHeaders,
|
|||
|
Style = "NormalTitle",
|
|||
|
ParagraphAlignment = PdfParagraphAlignmentType.Center,
|
|||
|
RowHeight = RowHeights[0]
|
|||
|
});
|
|||
|
|
|||
|
// Данные
|
|||
|
foreach (var Object in Data)
|
|||
|
{
|
|||
|
List<string> Cells = new List<string>();
|
|||
|
|
|||
|
for (int ColumnIndex = 0; ColumnIndex < TableHeaders.Count; ++ColumnIndex)
|
|||
|
{
|
|||
|
string PropertyName = HeaderPropertyNames[ColumnIndex];
|
|||
|
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);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|