140 lines
4.4 KiB
C#
140 lines
4.4 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Diagnostics;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using Controls.Models;
|
||
using MigraDoc.DocumentObjectModel;
|
||
using MigraDoc.Rendering;
|
||
|
||
namespace Controls
|
||
{
|
||
public partial class TableComponent : Component
|
||
{
|
||
private Document? _document;
|
||
|
||
private Section? _section;
|
||
|
||
public TableComponent()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
public TableComponent(IContainer container)
|
||
{
|
||
container.Add(this);
|
||
|
||
InitializeComponent();
|
||
}
|
||
|
||
[Obsolete]
|
||
public void CreateTable<T>(string docPath, string title, List<MergeCells>? mergeCells, List<ColumnInfo> colInfo, List<T> data) where T : class, new()
|
||
{
|
||
if (string.IsNullOrEmpty(docPath))
|
||
{
|
||
throw new ArgumentNullException("Введите путь до файла!");
|
||
}
|
||
if (string.IsNullOrEmpty(title))
|
||
{
|
||
throw new ArgumentNullException("Введите заголовок");
|
||
}
|
||
if (colInfo == null)
|
||
{
|
||
throw new ArgumentNullException("Введите все заголовки");
|
||
}
|
||
if (data == null)
|
||
{
|
||
throw new ArgumentNullException("Нету информации для вывода");
|
||
}
|
||
|
||
_document = new Document();
|
||
var style = _document.Styles["Normal"];
|
||
style.Font.Name = "Times New Roman";
|
||
style.Font.Size = 14;
|
||
|
||
_section = _document.AddSection();
|
||
|
||
var paragraph = _section.AddParagraph(title);
|
||
paragraph.Format.SpaceAfter = "0.3cm";
|
||
|
||
var table = _section.AddTable();
|
||
table.Borders.Visible = true;
|
||
|
||
for (int i = 0; i < colInfo.Count; i++)
|
||
{
|
||
table.AddColumn(colInfo[i].Width);
|
||
}
|
||
|
||
if (mergeCells != null)
|
||
{
|
||
table.AddRow();
|
||
}
|
||
var row = table.AddRow();
|
||
|
||
for (int i = 0; i < colInfo.Count; i++)
|
||
{
|
||
row[i].AddParagraph(colInfo[i].Header);
|
||
}
|
||
|
||
List<int> MergeColls = new List<int>();
|
||
|
||
if (mergeCells != null)
|
||
{
|
||
foreach (var cell in mergeCells)
|
||
{
|
||
MergeColls.AddRange(cell.Indexes[1..]);
|
||
table.Rows[cell.Indexes[0]].Cells[cell.Indexes[1] - 1].MergeRight = cell.Indexes[2..].Length;
|
||
table.Rows[cell.Indexes[0]].Cells[cell.Indexes[1] - 1].AddParagraph(cell.Header);
|
||
}
|
||
}
|
||
|
||
int cellsCount = table.Rows[1].Cells.Count;
|
||
|
||
if (MergeColls.Count != 0)
|
||
{
|
||
for (int i = 0; i < cellsCount; i++)
|
||
{
|
||
var cell = table.Rows[0].Cells[i];
|
||
if (!MergeColls.Contains(i + 1))
|
||
{
|
||
cell.MergeDown = 1;
|
||
cell.AddParagraph(colInfo[i].Header);
|
||
}
|
||
}
|
||
}
|
||
|
||
int rowData = 2;
|
||
|
||
foreach (var item in data)
|
||
{
|
||
var properties = item.GetType().GetProperties();
|
||
if (properties.Count() != cellsCount)
|
||
{
|
||
throw new Exception("Кол-во полей объекта не совпадает с кол-вом колонок");
|
||
}
|
||
|
||
for (int i = 0; i < properties.Count(); i++)
|
||
{
|
||
var property = properties[i];
|
||
var propValue = property.GetValue(item);
|
||
if (propValue == null) throw new Exception("Пустое поле");
|
||
if (property.Name == colInfo[i].PropertyName)
|
||
{
|
||
if (table.Rows.Count <= rowData) table.AddRow();
|
||
table.Rows[rowData].Cells[i].AddParagraph(propValue.ToString()!);
|
||
continue;
|
||
}
|
||
}
|
||
rowData++;
|
||
}
|
||
|
||
var renderer = new PdfDocumentRenderer(true);
|
||
renderer.Document = _document;
|
||
renderer.RenderDocument();
|
||
renderer.PdfDocument.Save(docPath);
|
||
}
|
||
}
|
||
}
|