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()); } } }