Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to create a table in a Word Document using OpenXML in C#. The code I'm using to create the table is:

C#
using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, true))
{
    Table table = new Table();

    TableProperties tblProp = new TableProperties(
        new TableBorders(
            new TopBorder()
            {
                Val = new EnumValue<BorderValues>(BorderValues.Dashed),
                Size = 24
            },
            new BottomBorder()
            {
                Val = new EnumValue<BorderValues>(BorderValues.Dashed),
                Size = 24
            },
            new LeftBorder()
            {
                Val = new EnumValue<BorderValues>(BorderValues.Dashed),
                Size = 24
            },
            new RightBorder()
            {
                Val = new EnumValue<BorderValues>(BorderValues.Dashed),
                Size = 24
            },
            new InsideHorizontalBorder()
            {
                Val = new EnumValue<BorderValues>(BorderValues.Dashed),
                Size = 24
            },
            new InsideVerticalBorder()
            {
                Val = new EnumValue<BorderValues>(BorderValues.Dashed),
                Size = 24
            }
        )
    );

    // Append the TableProperties object to the empty table.
    table.AppendChild<TableProperties>(tblProp);

    for (int i = 0; i < 6; i++)
    {
        // Create a Header row.
        TableRow tr = new TableRow();
        for (int j = 0; j < 6; j++)
        {
            // Create Cell
            TableCell tc = new TableCell(new Paragraph(new Run(new Text { Text = ""})));
            tr.Append(tc);
        }
        table.Append(tr);
        Debug.Print(table.OuterXml);
    } 
    doc.MainDocumentPart.Document
        .Body.Append(table);
    doc.Save();

The result of the Debug.Print(table.OuterXml) is:

XML
<w:tbl xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
	<w:tblPr>
		<w:tblBorders>
			<w:top w:val="dashed" w:sz="24"/>
			<w:bottom w:val="dashed" w:sz="24"/>
			<w:left w:val="dashed" w:sz="24"/>
			<w:right w:val="dashed" w:sz="24"/>
			<w:insideH w:val="dashed" w:sz="24"/>
			<w:insideV w:val="dashed" w:sz="24"/>
		</w:tblBorders>
	</w:tblPr>
	<w:tr>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
	</w:tr>
	<w:tr>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
	</w:tr>
	<w:tr>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
	</w:tr>
	<w:tr>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
		<w:tc>
			<w:p>
				<w:r>
					<w:t/>
				</w:r>
			</w:p>
		</w:tc>
	</w:tr>
</w:tbl>

Which looks ship-shape and Bristol fashion.

On the next line when I attempt to append the table to the document using doc.MainDocumentPart.Document.Body.Append(table);. But I get the following error:

C#
System.Xml.XmlException
  HResult=0x80131940
  Message=Unexpected end of file has occurred. The following elements are not closed: w:tr, w:tbl, w:body, w:document. Line 1, position 3232.

  This exception was originally thrown at this call stack:
    System.Xml.XmlTextReaderImpl.Throw(System.Exception)

The error is strange. I get the Line 1 but where is position 3232? The XML above is a result of pasting the debug print into Notepad++ and choosing the Pretty Print function so can't be relied on for positions and lines. The linearized XML has only 1 line only 1219 positions. Does anyone know what's going on here?

What I have tried:

C#
using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, true))
{
    Table table = new Table();

    TableProperties tblProp = new TableProperties(
        new TableBorders(
            new TopBorder()
            {
                Val = new EnumValue<BorderValues>(BorderValues.Dashed),
                Size = 24
            },
            new BottomBorder()
            {
                Val = new EnumValue<BorderValues>(BorderValues.Dashed),
                Size = 24
            },
            new LeftBorder()
            {
                Val = new EnumValue<BorderValues>(BorderValues.Dashed),
                Size = 24
            },
            new RightBorder()
            {
                Val = new EnumValue<BorderValues>(BorderValues.Dashed),
                Size = 24
            },
            new InsideHorizontalBorder()
            {
                Val = new EnumValue<BorderValues>(BorderValues.Dashed),
                Size = 24
            },
            new InsideVerticalBorder()
            {
                Val = new EnumValue<BorderValues>(BorderValues.Dashed),
                Size = 24
            }
        )
    );

    // Append the TableProperties object to the empty table.
    table.AppendChild<TableProperties>(tblProp);

    for (int i = 0; i < 6; i++)
    {
        // Create a Header row.
        TableRow tr = new TableRow();
        for (int j = 0; j < 6; j++)
        {
            // Create Cell
            TableCell tc = new TableCell(new Paragraph(new Run(new Text { Text = ""})));
            tr.Append(tc);
        }
        table.Append(tr);
        Debug.Print(table.OuterXml);
    } 
    doc.MainDocumentPart.Document
        .Body.Append(table);
    doc.Save();
Posted
Updated 9-Feb-22 9:40am
v2

1 solution

I discovered the issue. It wsa that the document that I was trying to update was corrupted. I had created the document earlier with a different code branch that was faulty which I wasn't aware of. I discovered the issue when i tried to open the document in Word and it produced an error. I deleted the document, created a new one and the code above ran beautifully.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900