VisualComponents/VisualComponentsLib/Components/ComponentBigTable.cs

235 lines
7.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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();
}
public void CreateDoc<T> (SetDataTable<T> setDataTable)
{
//создаём документ word
_wordDocument = WordprocessingDocument.Create(setDataTable.FilePath, WordprocessingDocumentType.Document);
//вытаскиваем главную часть из вордовского документа
MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart();
mainPart.Document = new Document();
//генерируем тело основной части документа
_docBody = mainPart.Document.AppendChild(new Body());
_wordDocument.Close();
AddTable<T> (setDataTable);
}
//создание таблицы
private void AddTable<T> (SetDataTable<T> setDataTable)
{
if (!CheckData(setDataTable.DataList))
{
throw new Exception("Не все ячейки заполнены");
}
using (var document = WordprocessingDocument.Open(setDataTable.FilePath, true))
{
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);
docRun.AppendChild(new Text(setDataTable.FileHeader));
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);
//генерация шапки таблицы
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++)
{
var tr = new TableRow();
foreach (var val in setDataTable.DataList[i].GetType().GetFields())
{
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.GetValue(setDataTable.DataList[i].GetType()).ToString()))));
tr.Append(tc);
table.Append(tr);
indexHeaderWidth++;
}
indexHeaderWidth = 0;
table.Append(tr);
}
#endregion
doc.Body.Append(header);
doc.Body.Append(table);
doc.Save();
}
}
//проверка заполненности входных значений
bool CheckData<T>(List<T> dataList)
{
foreach(var data in dataList)
{
foreach (var value in data.GetType().GetFields())
{
//для простоты проверки приводим значение каждого поля к типу string
if(string.IsNullOrEmpty(dataList.GetType().GetField(value.Name).GetValue(data).ToString()))
{
return false;
}
}
}
return true;
}
}
}