3
votes

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?

1
why word? why not excel?DLL_Whisperer
I wonder if I may be easier to write the whole file in c# instead of trying to fill out a template. you could make one class that is, in effect, the template, and it gets called by your function that has all the data fieldsKeith
Are you doing the word template on the server?Jeremy Thompson
@JeremyThompson yesaggicd
Kb257757 @aggicd. Instead use ClosedXML or Record a macro of word inserting the data, translate that to.netJeremy Thompson

1 Answers

0
votes

You will have to iterate over the field of the grid and insert them into the document using the aspose fields:

 Document wordDoc = new Document(myDoc);
 foreach (GridViewRow row in GridView1.Rows)
  {
      foreach (Microsoft.Office.Interop.Word.Field myMergeField in wordDoc.Fields)
      {
          //iterate over fields and save to apose:
           doc.MyField = row.MyRow; 
      }
   }
//save To Aspose

Aspose must know which of the fields you are going to populate, if any. Else, it won't export anything.

furthermore, word doesn't act kindly with grids. It won't let you export a table because it cannot create one, unlike excel. instead of inserting an entire grid, just populate each and every field.