2023-10-06 00:47:23 +04:00
|
|
|
|
using DocumentFormat.OpenXml.Packaging;
|
|
|
|
|
using DocumentFormat.OpenXml.Wordprocessing;
|
|
|
|
|
using DocumentFormat.OpenXml;
|
|
|
|
|
using System;
|
2023-09-27 13:52:25 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-10-06 00:47:23 +04:00
|
|
|
|
using VisualComponentsLib.Components.SupportClasses;
|
|
|
|
|
using DocumentFormat.OpenXml.EMMA;
|
2023-10-06 13:21:49 +04:00
|
|
|
|
using DocumentFormat.OpenXml.Drawing.Diagrams;
|
|
|
|
|
using FontFamily = DocumentFormat.OpenXml.Wordprocessing.FontFamily;
|
|
|
|
|
using System.Reflection.PortableExecutable;
|
2023-09-27 13:52:25 +04:00
|
|
|
|
|
|
|
|
|
namespace VisualComponentsLib.Components
|
|
|
|
|
{
|
|
|
|
|
public partial class ComponentWord : Component
|
|
|
|
|
{
|
2023-10-06 00:47:23 +04:00
|
|
|
|
private WordprocessingDocument? _wordDocument;
|
|
|
|
|
|
|
|
|
|
private Body? _docBody;
|
|
|
|
|
|
2023-09-27 13:52:25 +04:00
|
|
|
|
public ComponentWord()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ComponentWord(IContainer container)
|
|
|
|
|
{
|
|
|
|
|
container.Add(this);
|
|
|
|
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
2023-10-06 00:47:23 +04:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2023-10-06 13:21:49 +04:00
|
|
|
|
if (!CheckData(simpleTable.DataList))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Не все ячейки заполнены");
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 00:47:23 +04:00
|
|
|
|
using (var document = WordprocessingDocument.Open(simpleTable.FilePath, true))
|
|
|
|
|
{
|
|
|
|
|
var doc = document.MainDocumentPart.Document;
|
|
|
|
|
|
2023-10-06 13:21:49 +04:00
|
|
|
|
#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);
|
|
|
|
|
|
2023-10-09 18:14:48 +04:00
|
|
|
|
docRun.AppendChild(new Text(simpleTable.TableHeader));
|
2023-10-06 13:21:49 +04:00
|
|
|
|
|
|
|
|
|
header.AppendChild(docRun);
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Создание таблицы
|
2023-10-06 00:47:23 +04:00
|
|
|
|
|
|
|
|
|
Table table = new Table();
|
|
|
|
|
|
|
|
|
|
TableProperties props = new TableProperties(
|
|
|
|
|
new TableBorders(
|
|
|
|
|
new TopBorder
|
|
|
|
|
{
|
|
|
|
|
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
|
|
|
|
Size = 12
|
|
|
|
|
},
|
|
|
|
|
new BottomBorder
|
|
|
|
|
{
|
|
|
|
|
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
|
|
|
|
Size = 12
|
|
|
|
|
},
|
|
|
|
|
new LeftBorder
|
|
|
|
|
{
|
|
|
|
|
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
|
|
|
|
Size = 12
|
|
|
|
|
},
|
|
|
|
|
new RightBorder
|
2023-10-06 13:21:49 +04:00
|
|
|
|
{
|
|
|
|
|
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
|
|
|
|
Size = 12
|
|
|
|
|
},
|
|
|
|
|
new InsideHorizontalBorder
|
|
|
|
|
{
|
|
|
|
|
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
|
|
|
|
Size = 12
|
|
|
|
|
},
|
|
|
|
|
new InsideVerticalBorder
|
2023-10-06 00:47:23 +04:00
|
|
|
|
{
|
|
|
|
|
Val = new EnumValue<BorderValues>(BorderValues.Single),
|
|
|
|
|
Size = 12
|
|
|
|
|
}
|
2023-10-06 13:21:49 +04:00
|
|
|
|
));
|
2023-10-06 00:47:23 +04:00
|
|
|
|
|
|
|
|
|
table.AppendChild<TableProperties>(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();
|
|
|
|
|
|
2023-10-06 13:21:49 +04:00
|
|
|
|
tc.Append(new TableCellProperties(new TableCellWidth
|
|
|
|
|
{
|
|
|
|
|
Type = TableWidthUnitValues.Dxa,
|
|
|
|
|
Width = "2000"
|
|
|
|
|
}
|
|
|
|
|
));
|
2023-10-06 00:47:23 +04:00
|
|
|
|
|
2023-10-06 13:21:49 +04:00
|
|
|
|
tc.Append(new Paragraph(new Run(new Text(simpleTable.DataList[i][0, j]))));
|
2023-10-06 00:47:23 +04:00
|
|
|
|
|
|
|
|
|
tr.Append(tc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
table.Append(tr);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 13:21:49 +04:00
|
|
|
|
#endregion
|
|
|
|
|
|
2023-10-06 00:47:23 +04:00
|
|
|
|
doc.Body.Append(header);
|
|
|
|
|
|
|
|
|
|
doc.Body.Append(table);
|
|
|
|
|
|
|
|
|
|
doc.Save();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 13:21:49 +04:00
|
|
|
|
|
|
|
|
|
//проверка заполненности входных значений
|
|
|
|
|
bool CheckData(List<string[,]> 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;
|
|
|
|
|
}
|
2023-09-27 13:52:25 +04:00
|
|
|
|
}
|
|
|
|
|
}
|