using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
namespace ComponentsView
{
internal static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
//ApplicationConfiguration.Initialize();
//Application.Run(new FormComponents());
const string fileName = @"AddTable.docx";
const string fileUrl = @"\5semestr\KOP\KOP-PIbd-32-Katysheva-N-E";
AddTable();
static void AddTable() {
using (WordprocessingDocument document = WordprocessingDocument.Create(Path.Combine(fileUrl, fileName), WordprocessingDocumentType.Document))
{
//var doc = document.MainDocumentPart.Document;
MainDocumentPart mainPart = document.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = new Body();
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 headerRow = new TableRow();
for (var j = 0; j <= 3; j++)
{
var tc = new TableCell();
TableCellProperties cellProps = new TableCellProperties();
TableCellWidth cellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Auto };
cellProps.Append(cellWidth);
if (j == 0)
{
GridSpan gridSpan = new GridSpan() { Val = 3 };
cellProps.Append(gridSpan);
}
if (j == 1 || j == 2)
{
continue;
}
RunProperties headerRunProperties = new RunProperties();
Bold bold = new Bold();
headerRunProperties.Append(bold);
string headerText = "Column " + j;
Run run = new Run(new Text(headerText));
run.PrependChild(headerRunProperties);
tc.Append(new Paragraph(run));
tc.Append(cellProps);
headerRow.Append(tc);
}
table.Append(headerRow);
for (var i = 0; i <= 3; i++)
{
var tr = new TableRow();
for (var j = 0; j <= 3; j++)
{
var tc = new TableCell();
string data = "info" + i;
tc.Append(new Paragraph(new Run(new Text(data))));
tc.Append(new TableCellProperties(new TableCellWidth { Type = TableWidthUnitValues.Auto }));
tr.Append(tc);
}
table.Append(tr);
}
body.Append(table);
mainPart.Document.Append(body);
mainPart.Document.Save();
}
}
}
}
}