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:
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
}
)
);
table.AppendChild<TableProperties>(tblProp);
for (int i = 0; i < 6; i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < 6; j++)
{
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:
<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:
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:
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
}
)
);
table.AppendChild<TableProperties>(tblProp);
for (int i = 0; i < 6; i++)
{
TableRow tr = new TableRow();
for (int j = 0; j < 6; j++)
{
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();