I have created a word template with some number and text fields and I export my data from my application like this:
Object oMissing = System.Reflection.Missing.Value;
Object oTemplatePath = "C:\\MyTemplate.dotx";
Application wordApp = new Application();
Document wordDoc = new Document();
wordDoc = wordApp.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing);
foreach (Microsoft.Office.Interop.Word.Field myMergeField in wordDoc.Fields)
{
Range rngFieldCode = myMergeField.Code;
String fieldText = rngFieldCode.Text;
if (fieldText.StartsWith(" MERGEFIELD"))
{
Int32 endMerge = fieldText.IndexOf("\\");
Int32 fieldNameLength = fieldText.Length - endMerge;
String fieldName = fieldText.Substring(11, endMerge - 11);
fieldName = fieldName.Trim();
if (fieldName == "Name")
{
myMergeField.Select();
wordApp.Selection.TypeText(txtSponsorResp.Text.ToString());
}
//other fields .....
}
}
wordDoc.SaveAs("myFile.doc");
wordApp.Documents.Open("myFile.doc");
wordApp.Application.Quit();
And here is my code to export a grid to another word document:
System.Web.UI.WebControls.GridView GridView1 = new System.Web.UI.WebControls.GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = bud; //where bud contains the datasource
GridView1.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=Export-Grid.doc");
Response.Charset = "";
Response.ContentType = "application/vnd.word";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw);
Response.Write(sw.ToString());
Response.Flush();
Response.End();
How can I use both of them and export all the fields and the grids in one document using a word template?