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 DocumentFormat.OpenXml.EMMA; using DocumentFormat.OpenXml.Drawing.Diagrams; using FontFamily = DocumentFormat.OpenXml.Wordprocessing.FontFamily; using System.Reflection.PortableExecutable; namespace VisualComponentsLib.Components { public partial class ComponentWord : Component { private WordprocessingDocument? _wordDocument; private Body? _docBody; public ComponentWord() { InitializeComponent(); } public ComponentWord(IContainer container) { container.Add(this); InitializeComponent(); } public void CreateDoc(SimpleTable simpleTable) { //создаём документ word _wordDocument = WordprocessingDocument.Create(simpleTable.FilePath, WordprocessingDocumentType.Document); //вытаскиваем главную часть из вордовского документа MainDocumentPart mainPart = _wordDocument.AddMainDocumentPart(); mainPart.Document = new Document(); //генерируем тело основной части документа _docBody = mainPart.Document.AppendChild(new Body()); _wordDocument.Close(); AddTable(simpleTable); } //создание таблицы private void AddTable(SimpleTable simpleTable) { if (!CheckData(simpleTable.DataList)) { throw new Exception("Не все ячейки заполнены"); } using (var document = WordprocessingDocument.Open(simpleTable.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(simpleTable.TableHeader)); 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); for (var i = 0; i < simpleTable.DataList.Count; i++) { var tr = new TableRow(); for (var j = 0; j < simpleTable.DataList[i].Length; j++) { var tc = new TableCell(); tc.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Dxa, Width = "2000" } )); tc.Append(new Paragraph(new Run(new Text(simpleTable.DataList[i][0, j])))); tr.Append(tc); } table.Append(tr); } #endregion doc.Body.Append(header); doc.Body.Append(table); doc.Save(); } } //проверка заполненности входных значений bool CheckData(List data) { for (int i = 0; i < data.Count; i++) { for (int j = 0; j < data[i].Length; j++) { if (string.IsNullOrEmpty(data[i][0, j])) return false; } } return true; } } }