Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • AVERAGE 
  • COUNT 
  • COUNTA 
  • MAX 
  • MIN 
  • PRODUCT 
  • STDEV 
  • STDEVP 
  • SUM 
  • VAR 
  • VARP

Code

Code Block

public void GenerateDocument()
        {
            WordTemplate wt = new WordTemplate();
 
            // Create the array of mergefield names
            string[] fields = new string[] { "TestDate", "School", "Town", "Version" };
 
            // Create the array of values, each of which corresponds to a mergefield above
            object[] values = new object[] {
            new DateTime(2009, 11, 16).ToShortDateString(),"Samuel Adams HS", "Milbury", wt.Version};
 
            // Open the template document
            wt.Open(@"..\..\WordTemplateFiles\TestScoreReportTemplate.docx");
 
            // Set the main document data source
            wt.SetDataSource(values, fields, "");
 
            // Retrieve test score data
            DataTable dt = GetScores();
 
            // In a repeat block, a new entry is inserted for each row in the
            // data source.  When the data source is a DataTable, the mergefield
            // names are the same as the column names; so there is no need to
            // specify mergefield names as we did above.  However, we do have to set
            //a bookmark, "Items", so that wordwriter knows where to repeatedly bind the data.
            wt.SetRepeatBlock(dt, "Items");
 
            // Populate the template
            wt.Process();
 
            // Save the document to the disc in the desired location
            wt.Save(@"..\..\WordOutputFiles\ScoreReport_output.docx");
        }

Panel

 Template: TestScoreReportTemplate.docx

Output: ScoreReport_output.docx