KOP-PIbd-32-Katysheva-N-E/ComponentsView/Program.cs

132 lines
5.0 KiB
C#
Raw Normal View History

2024-10-15 11:27:49 +04:00
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
2024-09-30 22:43:40 +04:00
namespace ComponentsView
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
2024-10-15 11:27:49 +04:00
// 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>(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
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideHorizontalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
},
new InsideVerticalBorder
{
Val = new EnumValue<BorderValues>(BorderValues.Single),
Size = 12
}
)
);
table.AppendChild<TableProperties>(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<RunProperties>(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();
}
}
}
2024-09-30 22:43:40 +04:00
}
}