I have a GridView and I want to export it in Word document using open XML table
var dt = FunctionThatReturnDataTable(id);
var gd = new GridView
{
DataSource = dt,
AutoGenerateColumns = true
};
I created a word document and inserted one paragraph inside, now i want to add my gridview below
using (var myDoc = WordprocessingDocument.Create(@"c:test.doc",
WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart = myDoc.AddMainDocumentPart();
mainPart.Document = new Document();
var body = new Body();
var p = new Paragraph(new ParagraphProperties(
new Justification() { Val = JustificationValues.Center }),
new Run(new Text("Some Text")));
body.Append(p);
mainPart.Document.Append(body);
mainPart.Document.Save();
}
I can think of two different approaches to export
a ASP.Net
GridViewto a word document:The first approach renders the HTML of the
GridViewcontrol into amemory stream and then uses the openxml
altChunkelement to import thegenerated HTML into the word document. In short the
altChunkmarkupelement instructs MS Word to import content (e.g. HTML) into the
document. For an example, see the following code:
The second approach is more complex. We use the classes provided by the openxml SDK to
build the table. Please see the following code and the inline comments for further explanation.
The resulting xml produced by the two approaches is rather different.
Use a tool like the OpenXML SDK Productivity tool to view the generated xml.
In the
altChunkcase you do not see the definition for the table in the generated xml.