Промежуточное глобальное. Искренне надеюсь, что сработает (но нет)

This commit is contained in:
ElEgEv 2023-10-09 19:17:33 +04:00
parent 5b8fe3a26e
commit 056880251c
2 changed files with 75 additions and 22 deletions

View File

@ -30,10 +30,10 @@ namespace VisualComponentsLib.Components
InitializeComponent(); InitializeComponent();
} }
public void CreateDoc(SimpleTable simpleTable) public void CreateDoc<T> (SetDataTable<T> setDataTable)
{ {
//создаём документ word //создаём документ word
_wordDocument = WordprocessingDocument.Create(simpleTable.FilePath, WordprocessingDocumentType.Document); _wordDocument = WordprocessingDocument.Create(setDataTable.FilePath, WordprocessingDocumentType.Document);
//вытаскиваем главную часть из вордовского документа //вытаскиваем главную часть из вордовского документа
MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart(); MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart();
@ -45,18 +45,18 @@ namespace VisualComponentsLib.Components
_wordDocument.Close(); _wordDocument.Close();
AddTable(simpleTable); AddTable<T> (setDataTable);
} }
//создание таблицы //создание таблицы
private void AddTable(SimpleTable simpleTable) private void AddTable<T> (SetDataTable<T> setDataTable)
{ {
if (!CheckData(simpleTable.DataList)) if (!CheckData(setDataTable.DataList))
{ {
throw new Exception("Не все ячейки заполнены"); throw new Exception("Не все ячейки заполнены");
} }
using (var document = WordprocessingDocument.Open(simpleTable.FilePath, true)) using (var document = WordprocessingDocument.Open(setDataTable.FilePath, true))
{ {
var doc = document.MainDocumentPart.Document; var doc = document.MainDocumentPart.Document;
@ -88,7 +88,7 @@ namespace VisualComponentsLib.Components
docRun.AppendChild(properties); docRun.AppendChild(properties);
docRun.AppendChild(new Text(simpleTable.TableName)); docRun.AppendChild(new Text(setDataTable.FileHeader));
header.AppendChild(docRun); header.AppendChild(docRun);
@ -134,26 +134,72 @@ namespace VisualComponentsLib.Components
table.AppendChild<TableProperties>(props); table.AppendChild<TableProperties>(props);
for (var i = 0; i < simpleTable.DataList.Count; i++) //генерация шапки таблицы
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(); var tr = new TableRow();
for (var j = 0; j < simpleTable.DataList[i].Length; j++) 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(); var tc = new TableCell();
tc.Append(new TableCellProperties(new TableCellWidth tc.Append(new TableCellProperties(new TableCellWidth
{ {
Type = TableWidthUnitValues.Dxa, Type = TableWidthUnitValues.Dxa,
Width = "2000" Width = setDataTable.WidthCol[indexHeaderWidth].ToString(),
} }
)); ));
tc.Append(new Paragraph(new Run(new Text(simpleTable.DataList[i][0, j])))); tc.Append(new Paragraph(new Run(new Text(val.GetValue(setDataTable.DataList[i].GetType()).ToString()))));
tr.Append(tc); tr.Append(tc);
table.Append(tr);
indexHeaderWidth++;
} }
indexHeaderWidth = 0;
table.Append(tr); table.Append(tr);
} }
@ -172,10 +218,10 @@ namespace VisualComponentsLib.Components
{ {
foreach(var data in dataList) foreach(var data in dataList)
{ {
foreach (var value in data.GetType().GetProperties()) foreach (var value in data.GetType().GetFields())
{ {
//для простоты проверки приводим значение каждого поля к типу string //для простоты проверки приводим значение каждого поля к типу string
if(string.IsNullOrEmpty(value.ToString())) if(string.IsNullOrEmpty(dataList.GetType().GetField(value.Name).GetValue(data).ToString()))
{ {
return false; return false;
} }

View File

@ -1,4 +1,5 @@
using System; using DocumentFormat.OpenXml.Wordprocessing;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -13,26 +14,32 @@ namespace VisualComponentsLib.Components.SupportClasses
public string FileHeader = string.Empty; public string FileHeader = string.Empty;
//высота столбцов в заголовке
public List<double> HeightHeaderRow = new();
//высота столбцов //высота столбцов
public List<double> HeightSimpleRow = new(); public List<double> HeightRow = new();
//высота колонок //высота колонок
public List<double> WidthCol = new(); public List<double> WidthCol = new();
public List<T> DataList; public List<T> DataList;
public SetDataTable(string filePath, string fileHeader, List<double> heightHeaderRow, public SetDataTable(string filePath, string fileHeader, List<double> heightRow,
List<double> heightSimpleRow, List<double> widthCol, List<T> dataList) List<double> widthCol, List<T> dataList)
{ {
FilePath = filePath; FilePath = filePath;
FileHeader = fileHeader; FileHeader = fileHeader;
HeightHeaderRow = heightHeaderRow; HeightRow = heightRow;
HeightSimpleRow = heightSimpleRow;
WidthCol = widthCol; WidthCol = widthCol;
DataList = dataList; DataList = dataList;
DataList = GroupValue(DataList);
}
//группировка элементов списка по первому полю
private static List<T> GroupValue<T>(List<T> data)
{
var mainField = data[0].GetType().GetProperties().First().Name;
return (List<T>)data.GroupBy(field => field.GetType().GetProperties().First().Name).Select(field => field.ToList());
} }
} }
} }