2023-10-07 00:47:07 +04:00
|
|
|
|
using DocumentFormat.OpenXml.Packaging;
|
|
|
|
|
using DocumentFormat.OpenXml.Wordprocessing;
|
|
|
|
|
using DocumentFormat.OpenXml;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using VisualComponentsLib.Components.SupportClasses;
|
|
|
|
|
|
|
|
|
|
namespace VisualComponentsLib.Components
|
|
|
|
|
{
|
|
|
|
|
public partial class ComponentBigTable : Component
|
|
|
|
|
{
|
|
|
|
|
private WordprocessingDocument? _wordDocument;
|
|
|
|
|
|
|
|
|
|
private Body? _docBody;
|
|
|
|
|
|
|
|
|
|
public ComponentBigTable()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ComponentBigTable(IContainer container)
|
|
|
|
|
{
|
|
|
|
|
container.Add(this);
|
|
|
|
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-09 19:17:33 +04:00
|
|
|
|
public void CreateDoc<T> (SetDataTable<T> setDataTable)
|
2023-10-07 00:47:07 +04:00
|
|
|
|
{
|
|
|
|
|
//создаём документ word
|
2023-10-09 19:17:33 +04:00
|
|
|
|
_wordDocument = WordprocessingDocument.Create(setDataTable.FilePath, WordprocessingDocumentType.Document);
|
2023-10-07 00:47:07 +04:00
|
|
|
|
|
|
|
|
|
//вытаскиваем главную часть из вордовского документа
|
|
|
|
|
MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart();
|
|
|
|
|
|
|
|
|
|
mainPart.Document = new Document();
|
|
|
|
|
|
|
|
|
|
//генерируем тело основной части документа
|
|
|
|
|
_docBody = mainPart.Document.AppendChild(new Body());
|
|
|
|
|
|
|
|
|
|
_wordDocument.Close();
|
|
|
|
|
|
2023-10-09 19:17:33 +04:00
|
|
|
|
AddTable<T> (setDataTable);
|
2023-10-07 00:47:07 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//создание таблицы
|
2023-10-09 19:17:33 +04:00
|
|
|
|
private void AddTable<T> (SetDataTable<T> setDataTable)
|
2023-10-07 00:47:07 +04:00
|
|
|
|
{
|
2023-10-09 19:17:33 +04:00
|
|
|
|
if (!CheckData(setDataTable.DataList))
|
2023-10-07 00:47:07 +04:00
|
|
|
|
{
|
|
|
|
|
throw new Exception("Не все ячейки заполнены");
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-09 19:17:33 +04:00
|
|
|
|
using (var document = WordprocessingDocument.Open(setDataTable.FilePath, true))
|
2023-10-07 00:47:07 +04:00
|
|
|
|
{
|
|
|
|
|
var doc = document.MainDocumentPart.Document;
|
|
|
|
|
|
|
|
|
|
#region Создание заголовка
|
|
|
|
|
|
|
|
|
|
ParagraphProperties paragraphProperties = new();
|
|
|
|
|
|
|
|
|
|
paragraphProperties.AppendChild(new Justification
|
|
|
|
|
{
|
|
|
|
|
Val = JustificationValues.Center
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
paragraphProperties.AppendChild(new Indentation());
|
|
|
|
|
|
|
|
|
|
Paragraph header = new();
|
|
|
|
|
|
|
|
|
|
header.AppendChild(paragraphProperties);
|
|
|
|
|
|
|
|
|
|
var docRun = new Run();
|
|
|
|
|
|
|
|
|
|
var properties = new RunProperties();
|
|
|
|
|
|
|
|
|
|
properties.AppendChild(new FontSize
|
|
|
|
|
{
|
|
|
|
|
Val = "48"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
properties.AppendChild(new Bold());
|
|
|
|
|
|
|
|
|
|
docRun.AppendChild(properties);
|
|
|
|
|
|
2023-10-09 19:17:33 +04:00
|
|
|
|
docRun.AppendChild(new Text(setDataTable.FileHeader));
|
2023-10-07 00:47:07 +04:00
|
|
|
|
|
|
|
|
|
header.AppendChild(docRun);
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Создание таблицы
|
|
|
|
|
|
|
|
|
|
Table table = new Table();
|
|
|
|
|
|
|
|
|
|
TableProperties props = new TableProperties(
|
|
|
|
|
new TableBorders(
|
|
|
|
|
new TopBorder
|
|
|
|
|
{
|
|
|
|
|
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
|
|
|
|
Size = 12
|
|
|
|
|
},
|
|
|
|
|
new BottomBorder
|
|
|
|
|
{
|
|
|
|
|
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
|
|
|
|
Size = 12
|
|
|
|
|
},
|
|
|
|
|
new LeftBorder
|
|
|
|
|
{
|
|
|
|
|
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
|
|
|
|
Size = 12
|
|
|
|
|
},
|
|
|
|
|
new RightBorder
|
|
|
|
|
{
|
|
|
|
|
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
|
|
|
|
Size = 12
|
|
|
|
|
},
|
|
|
|
|
new InsideHorizontalBorder
|
|
|
|
|
{
|
|
|
|
|
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
|
|
|
|
Size = 12
|
|
|
|
|
},
|
|
|
|
|
new InsideVerticalBorder
|
|
|
|
|
{
|
|
|
|
|
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
|
|
|
|
Size = 12
|
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
table.AppendChild<TableProperties>(props);
|
|
|
|
|
|
2023-10-09 19:17:33 +04:00
|
|
|
|
//генерация шапки таблицы
|
|
|
|
|
var _tr = new TableRow();
|
|
|
|
|
|
|
|
|
|
int indexHeaderHeigh = 0;
|
|
|
|
|
int indexHeaderWidth = 0;
|
|
|
|
|
|
|
|
|
|
foreach (var val in setDataTable.DataList[0].GetType().GetProperties())
|
|
|
|
|
{
|
|
|
|
|
_tr.Append(new TableRowProperties(new TableRowHeight
|
|
|
|
|
{
|
|
|
|
|
Val = Convert.ToUInt32(setDataTable.HeightRow[indexHeaderHeigh])
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
var tc = new TableCell();
|
|
|
|
|
|
|
|
|
|
tc.Append(new TableCellProperties(new TableCellWidth
|
|
|
|
|
{
|
|
|
|
|
Type = TableWidthUnitValues.Dxa,
|
|
|
|
|
Width = setDataTable.WidthCol[indexHeaderWidth].ToString(),
|
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
tc.Append(new Paragraph(new Run(new Text(val.Name))));
|
|
|
|
|
|
|
|
|
|
_tr.Append(tc);
|
|
|
|
|
|
|
|
|
|
table.Append(_tr);
|
|
|
|
|
|
|
|
|
|
indexHeaderWidth++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//увеличиваем счётчик на 1, т. к. в списке только два значения
|
|
|
|
|
indexHeaderHeigh++;
|
|
|
|
|
|
|
|
|
|
//генерация тела таблицы
|
|
|
|
|
for (var i = 1; i < setDataTable.DataList.Count; i++)
|
2023-10-07 00:47:07 +04:00
|
|
|
|
{
|
|
|
|
|
var tr = new TableRow();
|
|
|
|
|
|
2023-10-09 19:17:33 +04:00
|
|
|
|
foreach (var val in setDataTable.DataList[i].GetType().GetFields())
|
2023-10-07 00:47:07 +04:00
|
|
|
|
{
|
2023-10-09 19:17:33 +04:00
|
|
|
|
tr.Append(new TableRowProperties(new TableRowHeight
|
|
|
|
|
{
|
|
|
|
|
Val = Convert.ToUInt32(setDataTable.HeightRow[indexHeaderHeigh])
|
|
|
|
|
}));
|
|
|
|
|
|
2023-10-07 00:47:07 +04:00
|
|
|
|
var tc = new TableCell();
|
|
|
|
|
|
|
|
|
|
tc.Append(new TableCellProperties(new TableCellWidth
|
|
|
|
|
{
|
|
|
|
|
Type = TableWidthUnitValues.Dxa,
|
2023-10-09 19:17:33 +04:00
|
|
|
|
Width = setDataTable.WidthCol[indexHeaderWidth].ToString(),
|
2023-10-07 00:47:07 +04:00
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
|
2023-10-09 19:17:33 +04:00
|
|
|
|
tc.Append(new Paragraph(new Run(new Text(val.GetValue(setDataTable.DataList[i].GetType()).ToString()))));
|
2023-10-07 00:47:07 +04:00
|
|
|
|
|
|
|
|
|
tr.Append(tc);
|
2023-10-09 19:17:33 +04:00
|
|
|
|
|
|
|
|
|
table.Append(tr);
|
|
|
|
|
|
|
|
|
|
indexHeaderWidth++;
|
2023-10-07 00:47:07 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-09 19:17:33 +04:00
|
|
|
|
indexHeaderWidth = 0;
|
|
|
|
|
|
2023-10-07 00:47:07 +04:00
|
|
|
|
table.Append(tr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
doc.Body.Append(header);
|
|
|
|
|
|
|
|
|
|
doc.Body.Append(table);
|
|
|
|
|
|
|
|
|
|
doc.Save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//проверка заполненности входных значений
|
2023-10-09 18:14:48 +04:00
|
|
|
|
bool CheckData<T>(List<T> dataList)
|
2023-10-07 00:47:07 +04:00
|
|
|
|
{
|
2023-10-09 18:14:48 +04:00
|
|
|
|
foreach(var data in dataList)
|
2023-10-07 00:47:07 +04:00
|
|
|
|
{
|
2023-10-09 19:17:33 +04:00
|
|
|
|
foreach (var value in data.GetType().GetFields())
|
2023-10-07 00:47:07 +04:00
|
|
|
|
{
|
2023-10-09 18:14:48 +04:00
|
|
|
|
//для простоты проверки приводим значение каждого поля к типу string
|
2023-10-09 19:17:33 +04:00
|
|
|
|
if(string.IsNullOrEmpty(dataList.GetType().GetField(value.Name).GetValue(data).ToString()))
|
2023-10-09 18:14:48 +04:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-10-07 00:47:07 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|