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; using Spire.Doc.Formatting; using System.CodeDom; using DocumentFormat.OpenXml.Vml; 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 (SetDataTable 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 (setDataTable); } //создание таблицы private void AddTable (SetDataTable 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.Single), Size = 12 }, new BottomBorder { Val = new EnumValue(BorderValues.Single), Size = 12 }, new LeftBorder { Val = new EnumValue(BorderValues.Single), Size = 12 }, new RightBorder { Val = new EnumValue(BorderValues.Single), Size = 12 }, new InsideHorizontalBorder { Val = new EnumValue(BorderValues.Single), Size = 12 }, new InsideVerticalBorder { Val = new EnumValue(BorderValues.Single), Size = 12 } )); table.AppendChild(props); //генерация шапки таблицы var _tr = new TableRow(); int indexHeaderHeigh = 0; int indexHeaderWidth = 0; //пробегаемся по словарю, чтобы сразу заполнять данные в нужной последовательности foreach (var item in setDataTable.ColumnsSettings) { _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(), } )); if (string.IsNullOrEmpty(setDataTable.ColumnsSettings[indexHeaderWidth].NameField) || string.IsNullOrEmpty(setDataTable.ColumnsSettings[indexHeaderWidth].NameColumn)) { throw new Exception("Некорректное заполнение информации для шапки таблицы"); } tc.Append(new Paragraph(new Run(new Text(item.Value.NameColumn)))); _tr.Append(tc); indexHeaderWidth++; } /*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(), } )); if (string.IsNullOrEmpty(setDataTable.ColumnsSettings[indexHeaderWidth].NameField) || string.IsNullOrEmpty(setDataTable.ColumnsSettings[indexHeaderWidth].NameColumn)) { throw new Exception("Некорректное заполнение информации для шапки таблицы"); } tc.Append(new Paragraph(new Run(new Text(val.Name)))); _tr.Append(tc); indexHeaderWidth++; }*/ table.Append(_tr); //увеличиваем счётчик на 1, т. к. в списке только два значения indexHeaderHeigh++; indexHeaderWidth = 0; int numberColunm = 0; //генерация тела таблицы for (var i = 1; i < setDataTable.DataList.Count; i++) { var tr = new TableRow(); foreach (var val in setDataTable.DataList[i].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.GetValue(setDataTable.DataList[i]).ToString())))); tr.Append(tc); indexHeaderWidth++; } indexHeaderWidth = 0; table.Append(tr); } #endregion doc.Body.Append(header); doc.Body.Append(table); doc.Save(); } } //проверка заполненности входных значений bool CheckData(List dataList) { foreach(var data in dataList) { foreach (var value in data.GetType().GetProperties()) { //для простоты проверки приводим значение каждого поля к типу string if(string.IsNullOrEmpty(value.GetValue(data).ToString())) { return false; } } } return true; } } }