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;
|
2023-10-09 22:54:08 +04:00
|
|
|
|
using Spire.Doc.Formatting;
|
2023-10-10 00:38:20 +04:00
|
|
|
|
using System.CodeDom;
|
2023-10-11 00:09:17 +04:00
|
|
|
|
using DocumentFormat.OpenXml.Vml;
|
2023-10-07 00:47:07 +04:00
|
|
|
|
|
|
|
|
|
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-10 00:38:20 +04:00
|
|
|
|
if (!CheckData(setDataTable.DataList))
|
2023-10-07 00:47:07 +04:00
|
|
|
|
{
|
|
|
|
|
throw new Exception("Не все ячейки заполнены");
|
2023-10-10 00:38:20 +04:00
|
|
|
|
}
|
2023-10-07 00:47:07 +04:00
|
|
|
|
|
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"
|
|
|
|
|
});
|
|
|
|
|
|
2023-10-11 12:14:23 +04:00
|
|
|
|
properties.Append(new Bold());
|
2023-10-07 00:47:07 +04:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2023-10-11 00:09:17 +04:00
|
|
|
|
//пробегаемся по словарю, чтобы сразу заполнять данные в нужной последовательности
|
|
|
|
|
foreach (var item in setDataTable.ColumnsSettings)
|
2023-10-09 19:17:33 +04:00
|
|
|
|
{
|
|
|
|
|
_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(),
|
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
|
2023-10-11 00:09:17 +04:00
|
|
|
|
if (string.IsNullOrEmpty(setDataTable.ColumnsSettings[indexHeaderWidth].NameField) ||
|
|
|
|
|
string.IsNullOrEmpty(setDataTable.ColumnsSettings[indexHeaderWidth].NameColumn))
|
2023-10-10 00:38:20 +04:00
|
|
|
|
{
|
2023-10-11 00:09:17 +04:00
|
|
|
|
throw new Exception("Некорректное заполнение информации для шапки таблицы");
|
2023-10-10 00:38:20 +04:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-11 12:14:23 +04:00
|
|
|
|
Paragraph tableHeader = new();
|
|
|
|
|
|
|
|
|
|
var Run = new Run();
|
|
|
|
|
|
|
|
|
|
var headerProperties = new RunProperties();
|
|
|
|
|
|
|
|
|
|
headerProperties.Append(new Bold());
|
|
|
|
|
|
|
|
|
|
Run.AppendChild(headerProperties);
|
|
|
|
|
|
|
|
|
|
Run.AppendChild(new Text(item.Value.NameColumn));
|
|
|
|
|
|
|
|
|
|
tableHeader.AppendChild(Run);
|
|
|
|
|
|
|
|
|
|
tc.Append(tableHeader);
|
2023-10-09 19:17:33 +04:00
|
|
|
|
|
|
|
|
|
_tr.Append(tc);
|
|
|
|
|
|
|
|
|
|
indexHeaderWidth++;
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-09 22:54:08 +04:00
|
|
|
|
table.Append(_tr);
|
|
|
|
|
|
2023-10-09 19:17:33 +04:00
|
|
|
|
//увеличиваем счётчик на 1, т. к. в списке только два значения
|
|
|
|
|
indexHeaderHeigh++;
|
2023-10-09 22:54:08 +04:00
|
|
|
|
indexHeaderWidth = 0;
|
2023-10-10 00:38:20 +04:00
|
|
|
|
int numberColunm = 0;
|
2023-10-09 19:17:33 +04:00
|
|
|
|
|
|
|
|
|
//генерация тела таблицы
|
|
|
|
|
for (var i = 1; i < setDataTable.DataList.Count; i++)
|
2023-10-07 00:47:07 +04:00
|
|
|
|
{
|
|
|
|
|
var tr = new TableRow();
|
|
|
|
|
|
2023-10-11 10:59:52 +04:00
|
|
|
|
//пробегаемся по словарю, чтобы сразу добавлять значения в нужном порядке
|
|
|
|
|
foreach (var item in setDataTable.ColumnsSettings)
|
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-11 10:59:52 +04:00
|
|
|
|
//вытаскиваем нужное значение
|
|
|
|
|
foreach(var val in setDataTable.DataList[i].GetType().GetProperties())
|
|
|
|
|
{
|
|
|
|
|
if(val.Name == item.Value.NameField)
|
|
|
|
|
{
|
2023-10-11 12:14:23 +04:00
|
|
|
|
var newParagraph = new Paragraph();
|
|
|
|
|
|
|
|
|
|
var newRun = new Run();
|
|
|
|
|
|
|
|
|
|
var runProperties = new RunProperties();
|
|
|
|
|
|
|
|
|
|
if(indexHeaderWidth == 0)
|
|
|
|
|
{
|
|
|
|
|
runProperties.Append(new Bold());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newRun.AppendChild(runProperties);
|
|
|
|
|
|
|
|
|
|
newRun.AppendChild(new Text(val.GetValue(setDataTable.DataList[i]).ToString()));
|
|
|
|
|
|
|
|
|
|
newParagraph.AppendChild(newRun);
|
|
|
|
|
|
|
|
|
|
tc.Append(newParagraph);
|
2023-10-11 10:59:52 +04:00
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-07 00:47:07 +04:00
|
|
|
|
|
|
|
|
|
tr.Append(tc);
|
2023-10-09 19:17:33 +04:00
|
|
|
|
|
|
|
|
|
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 22:54:08 +04:00
|
|
|
|
foreach (var value in data.GetType().GetProperties())
|
2023-10-07 00:47:07 +04:00
|
|
|
|
{
|
2023-10-09 18:14:48 +04:00
|
|
|
|
//для простоты проверки приводим значение каждого поля к типу string
|
2023-10-10 00:38:20 +04:00
|
|
|
|
if(string.IsNullOrEmpty(value.GetValue(data).ToString()))
|
2023-10-09 18:14:48 +04:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-10-07 00:47:07 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|