'OpenXML Create Word Document always corrupt C#
I am working on an old webforms application and I am trying to create a word document via the OpenXml in C# within one of the pages. I am trying to read in data from SQL to populate a table I will create in the word document by following the examples found at http://www.ludovicperrichon.com/create-a-word-document-with-openxml-and-c/
However, every time I run the code it produces a document that Word reports is corrupt and presents the following message: 
I cannot see what the issue is and I have been trying to find similar issues for a solution but I can only find mentions of a problem with the stream but this seems to be only when when reading an existing document to edit not creating a new one.
My example code is below (excluding the DataTable dt where the data is loaded, since that element works fine).
var header = "";
using (var stream = new MemoryStream())
{
using (var wordDoc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document))
{
// Add a main document part.
MainDocumentPart mainPart = wordDoc.AddMainDocumentPart();
// Create the document structure and add some text.
mainPart.Document = new Document();
Body docBody = new Body();
mainPart.Document.Body = docBody;
// Load study plan and build word table to hold data
// Create an empty table.
var table = new DocumentFormat.OpenXml.Drawing.Table();
// Create a TableProperties object and specify its border information.
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);
// Add header row
// Create a row.
var hr = new DocumentFormat.OpenXml.Drawing.TableRow();
// Create a cell.
var hc1 = new DocumentFormat.OpenXml.Drawing.TableCell();
// Specify the width property of the table cell.
hc1.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
// Specify the table cell content.
hc1.Append(new Paragraph(new Run(new Text("Start Date"))));
// Append the table cell to the table row.
hr.Append(hc1);
// Create a cell
var hc2 = new DocumentFormat.OpenXml.Drawing.TableCell();
hc2.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
hc2.Append(new Paragraph(new Run(new Text("End Date"))));
// Append the table cell to the table row.
hr.Append(hc2);
// Create a cell
var hc3 = new DocumentFormat.OpenXml.Drawing.TableCell();
hc3.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
hc3.Append(new Paragraph(new Run(new Text("Type"))));
// Append the table cell to the table row.
hr.Append(hc3);
// Create a cell
var hc4 = new DocumentFormat.OpenXml.Drawing.TableCell();
hc4.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
hc4.Append(new Paragraph(new Run(new Text("Status"))));
// Append the table cell to the table row.
hr.Append(hc4);
// Create a cell
var hc5 = new DocumentFormat.OpenXml.Drawing.TableCell();
hc5.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
hc5.Append(new Paragraph(new Run(new Text("Description"))));
// Append the table cell to the table row.
hr.Append(hc5);
// Append the table row to the table.
table.Append(hr);
// Add data rows
for (var i = 0; i < dt.Rows.Count; i++)
{
// Create a row.
var tr = new DocumentFormat.OpenXml.Drawing.TableRow();
// Create a cell.
var tc1 = new DocumentFormat.OpenXml.Drawing.TableCell();
// Specify the width property of the table cell.
tc1.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
// Specify the table cell content.
tc1.Append(new Paragraph(new Run(new Text(dt.Rows[i]["start"].ToString()))));
// Append the table cell to the table row.
tr.Append(tc1);
//Create a cell
var tc2 = new DocumentFormat.OpenXml.Drawing.TableCell();
tc2.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tc2.Append(new Paragraph(new Run(new Text(dt.Rows[i]["end"].ToString()))));
//Append the table cell to the table row.
tr.Append(tc2);
///Create a cell
var tc3 = new DocumentFormat.OpenXml.Drawing.TableCell();
tc3.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tc3.Append(new Paragraph(new Run(new Text(dt.Rows[i]["type"].ToString()))));
//Append the table cell to the table row.
tr.Append(tc3);
// Create a cell
var tc4 = new DocumentFormat.OpenXml.Drawing.TableCell();
tc4.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tc4.Append(new Paragraph(new Run(new Text(dt.Rows[i]["status"].ToString()))));
//Append the table cell to the table row.
tr.Append(tc4);
//Create a cell
var tc5 = new DocumentFormat.OpenXml.Drawing.TableCell();
tc5.Append(new TableCellProperties(
new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = "2400" }));
tc5.Append(new Paragraph(new Run(new Text(dt.Rows[i]["description"].ToString()))));
//Append the table cell to the table row.
tr.Append(tc5);
//Append the table row to the table.
table.Append(tr);
}
//Append the table to the document.
wordDoc.MainDocumentPart.Document.Body.Append(table);
header = "attachment;filename=" + DateTime.Today.ToString("dd/MM/yyyy") + student.Replace(" ", "_") + "_study_plan.docx";
}
Context.Response.AppendHeader("Content-Disposition", header);
stream.Position = 0;
stream.CopyTo(Context.Response.OutputStream);
Context.Response.Flush();
Context.Response.End();
}
Solution 1:[1]
You are using the tableelements from the wrong namespace. Instead of
var table = new DocumentFormat.OpenXml.Drawing.Table();
use
var table = new DocumentFormat.OpenXml.Wordprocessing.Table();
This goes for all the other table elements (cells etc) as well.
I recommend using OpenXMLSDK. If you end up with an invalid file it will show you what's wrong with it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | StefanFFM |
