From 056880251c098e30aeea045a27d0f98b81e463a1 Mon Sep 17 00:00:00 2001 From: ElEgEv <112943269+ElEgEv@users.noreply.github.com> Date: Mon, 9 Oct 2023 19:17:33 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=BC=D0=B5=D0=B6=D1=83?= =?UTF-8?q?=D1=82=D0=BE=D1=87=D0=BD=D0=BE=D0=B5=20=D0=B3=D0=BB=D0=BE=D0=B1?= =?UTF-8?q?=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=D0=B5.=20=D0=98=D1=81=D0=BA?= =?UTF-8?q?=D1=80=D0=B5=D0=BD=D0=BD=D0=B5=20=D0=BD=D0=B0=D0=B4=D0=B5=D1=8E?= =?UTF-8?q?=D1=81=D1=8C,=20=D1=87=D1=82=D0=BE=20=D1=81=D1=80=D0=B0=D0=B1?= =?UTF-8?q?=D0=BE=D1=82=D0=B0=D0=B5=D1=82=20(=D0=BD=D0=BE=20=D0=BD=D0=B5?= =?UTF-8?q?=D1=82)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/ComponentBigTable.cs | 72 +++++++++++++++---- .../Components/SupportClasses/SetDataTable.cs | 25 ++++--- 2 files changed, 75 insertions(+), 22 deletions(-) diff --git a/VisualComponentsLib/Components/ComponentBigTable.cs b/VisualComponentsLib/Components/ComponentBigTable.cs index e219476..8843fa3 100644 --- a/VisualComponentsLib/Components/ComponentBigTable.cs +++ b/VisualComponentsLib/Components/ComponentBigTable.cs @@ -30,10 +30,10 @@ namespace VisualComponentsLib.Components InitializeComponent(); } - public void CreateDoc(SimpleTable simpleTable) + public void CreateDoc (SetDataTable setDataTable) { //создаём документ word - _wordDocument = WordprocessingDocument.Create(simpleTable.FilePath, WordprocessingDocumentType.Document); + _wordDocument = WordprocessingDocument.Create(setDataTable.FilePath, WordprocessingDocumentType.Document); //вытаскиваем главную часть из вордовского документа MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart(); @@ -45,18 +45,18 @@ namespace VisualComponentsLib.Components _wordDocument.Close(); - AddTable(simpleTable); + AddTable (setDataTable); } //создание таблицы - private void AddTable(SimpleTable simpleTable) + private void AddTable (SetDataTable setDataTable) { - if (!CheckData(simpleTable.DataList)) + if (!CheckData(setDataTable.DataList)) { throw new Exception("Не все ячейки заполнены"); } - using (var document = WordprocessingDocument.Open(simpleTable.FilePath, true)) + using (var document = WordprocessingDocument.Open(setDataTable.FilePath, true)) { var doc = document.MainDocumentPart.Document; @@ -88,7 +88,7 @@ namespace VisualComponentsLib.Components docRun.AppendChild(properties); - docRun.AppendChild(new Text(simpleTable.TableName)); + docRun.AppendChild(new Text(setDataTable.FileHeader)); header.AppendChild(docRun); @@ -134,26 +134,72 @@ namespace VisualComponentsLib.Components table.AppendChild(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(); - 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(); tc.Append(new TableCellProperties(new TableCellWidth { 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); + + table.Append(tr); + + indexHeaderWidth++; } + indexHeaderWidth = 0; + table.Append(tr); } @@ -172,10 +218,10 @@ namespace VisualComponentsLib.Components { foreach(var data in dataList) { - foreach (var value in data.GetType().GetProperties()) + foreach (var value in data.GetType().GetFields()) { //для простоты проверки приводим значение каждого поля к типу string - if(string.IsNullOrEmpty(value.ToString())) + if(string.IsNullOrEmpty(dataList.GetType().GetField(value.Name).GetValue(data).ToString())) { return false; } diff --git a/VisualComponentsLib/Components/SupportClasses/SetDataTable.cs b/VisualComponentsLib/Components/SupportClasses/SetDataTable.cs index f7f0253..80e4ed5 100644 --- a/VisualComponentsLib/Components/SupportClasses/SetDataTable.cs +++ b/VisualComponentsLib/Components/SupportClasses/SetDataTable.cs @@ -1,4 +1,5 @@ -using System; +using DocumentFormat.OpenXml.Wordprocessing; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -13,26 +14,32 @@ namespace VisualComponentsLib.Components.SupportClasses public string FileHeader = string.Empty; - //высота столбцов в заголовке - public List HeightHeaderRow = new(); - //высота столбцов - public List HeightSimpleRow = new(); + public List HeightRow = new(); //высота колонок public List WidthCol = new(); public List DataList; - public SetDataTable(string filePath, string fileHeader, List heightHeaderRow, - List heightSimpleRow, List widthCol, List dataList) + public SetDataTable(string filePath, string fileHeader, List heightRow, + List widthCol, List dataList) { FilePath = filePath; FileHeader = fileHeader; - HeightHeaderRow = heightHeaderRow; - HeightSimpleRow = heightSimpleRow; + HeightRow = heightRow; WidthCol = widthCol; DataList = dataList; + + DataList = GroupValue(DataList); + } + + //группировка элементов списка по первому полю + private static List GroupValue(List data) + { + var mainField = data[0].GetType().GetProperties().First().Name; + + return (List)data.GroupBy(field => field.GetType().GetProperties().First().Name).Select(field => field.ToList()); } } }