Open XML: Create a table

Within this article I want to show you how to create a table within a word document.

 
Table structure

In Open XML documents there are a lot of different content elements and styles. Within this article I want to show you how to define a simple table with some borders and text as content.

A table is a collection of rows. Each row itself is a collection of cells. In Open XML these elements are represented by Table, TableRow and TableCell objects.

As described in previous articles, the content of an Open XML document has to be placed in a Run and Paragraph element. So if you want to add text to a cell you have to define these elements. Therefore I have created the following helper function to create a TableCell element.

private static TableCell CreateCell(string text)
{
    return new TableCell(new Paragraph(new Run(new Text(text))));
}

 
Create a document with a table

The following source code shows an example console application. At first a Table is created. Then we add the header row and set the text for each column. Afterwards we add five rows to the table. At least the table is added to the document structure and a file will be written. Please note line 13 with the SetTableStyle command. This is an additional function to set some table styles. I will show you the function within the next section.

static void Main(string[] args)
{
    Table table = null;
    TableRow row = null;            

    //file name
    string folder = @"...";
    string fileName = folder + @"\Test.docx";

    //create table
    table = new Table();

    SetTableStyle(table);

    //add first row with title            
    row = new TableRow();
    row.Append(CreateCell("Column A"));
    row.Append(CreateCell("Column B"));
    row.Append(CreateCell("Column C"));
            
    table.Append(row);

    //add content rows
    for (int rowNumber = 1; rowNumber <= 5; rowNumber++)
    {
        row = new TableRow();

        row.Append(CreateCell("A" + rowNumber.ToString()));
        row.Append(CreateCell("B" + rowNumber.ToString()));
        row.Append(CreateCell("C" + rowNumber.ToString()));                

        table.Append(row);
    }           

    //add table to body
    var body = new Body(table);
    var document = new Document(body);

    //create file
    using (var file = WordprocessingDocument.Create(
        fileName, WordprocessingDocumentType.Document))
    {
        file.AddMainDocumentPart();
        file.MainDocumentPart.Document = document;
        file.MainDocumentPart.Document.Save();
    }
}

private static TableCell CreateCell(string text)
{
    return new TableCell(new Paragraph(new Run(new Text(text))));
}

 
Table style

At the moment our table has no style. So the text is placed in a table grid but no borders are shown. As mentioned above I have added an additional function SetTableStyle to set some styles. The following source code shows this function. At first all borders are set. And as second style element the table width is set to the full page width.

private static void SetTableStyle(Table table)
{            
    TableProperties properties = new TableProperties();
            
    //table borders
    TableBorders borders = new TableBorders();

    borders.TopBorder = new TopBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single) };
    borders.BottomBorder = new BottomBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single) };
    borders.LeftBorder = new LeftBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single) };
    borders.RightBorder = new RightBorder() { Val = new EnumValue<BorderValues>(BorderValues.Single) };
    borders.InsideHorizontalBorder = new InsideHorizontalBorder() { Val = BorderValues.Single };
    borders.InsideVerticalBorder = new InsideVerticalBorder() { Val = BorderValues.Single };
            
    properties.Append(borders);

    //set the table width to page width
    TableWidth tableWidth = new TableWidth() { Width = "5000", Type = TableWidthUnitValues.Pct };
    properties.Append(tableWidth);

    //add properties to table
    table.Append(properties);        
}

 
Summary

Within this article you have seen how to create a simple table within an Open XML document. This article will allow you to getting started with this topic but of course it will only show the basic table style. You have a lot of additional possibilities to create more complex tables.

Werbung
Dieser Beitrag wurde unter .NET, C#, Open XML veröffentlicht. Setze ein Lesezeichen auf den Permalink.

Eine Antwort zu Open XML: Create a table

  1. Anonymous schreibt:

    Your code means nothing when you do not include used namecpaces

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit deinem WordPress.com-Konto. Abmelden /  Ändern )

Facebook-Foto

Du kommentierst mit deinem Facebook-Konto. Abmelden /  Ändern )

Verbinde mit %s