Beginning in WordWriter version 4.1, WordTemplate now has the ability to evaluate a certain number of formulas on data fields that are brought into a template.

Word formulas are only supported with the Word 2007 OOXML file format (.docx/.docm).

Code Sample: WordTemplate Formula Demo

A WordTemplate formulas sample will be available shortly from the SoftArtisans OfficeWriter Demo Center. Screenshots from the sample are used below.

Placing Formulas in Merge Fields

Formulas in a Merge Field in your template can be used to calculate average, products, sums, and other formulas based on values in the data fields being bound to the template.

To place a formula into your template document, insert a merge field with a value as following:

<<=FORMULA(DataSource.Field)>>

The following formulas are valid with WordTemplate and will be evaluated when the data are bound to the document: AVERAGE, COUNT, COUNTA, MAX, MIN, PRODUCT, STDEV, STDEVP, SUM, VAR, and VARP. Additional information is available on the Creating Merge Fields page.

The WordTemplate Formula Demo uses formulas to display the average, standard deviation, maximum, and minimum college entrance exam scores of a group of fictional students as part of a table including their individual scores. A repeat block on the first row allows WordTemplate to bring in all of the values of the referenced fields.

The following code opens the template and binds data to it using the SetDataSource and SetRepeatBlock methods, then streans the resulting document to the user:

protected void OnDownload(object sender, EventArgs e)
{
     WordTemplate wt = new WordTemplate();
     wt.Open(Page.MapPath("templates/TestScoreReport.docx"));

     string[] fields = new string[] { "TestDate", "School", "Town", "Version" };
     object[] values = new object[] { 
     new DateTime(2009, 11, 16).ToShortDateString(),
          "Samuel Adams HS", 
          "Milbury",
          wt.Version
          };
     wt.SetDataSource(values, fields, "");

     DataTable dt = GetScores();
     wt.SetRepeatBlock(dt, "Items");

     wt.Process();
     wt.Save(Response, "ScoreReport.docx", false);
}

private DataTable GetScores()
{
     DataTable dt = new DataTable();

     dt.Columns.Add("StudentID", typeof(string));
     dt.Columns.Add("ReadingScore", typeof(int));
     dt.Columns.Add("WritingScore", typeof(int));
     dt.Columns.Add("MathScore", typeof(int));
     dt.Columns.Add("TotalScore", typeof(int));

     Random rand = new Random();

     int rScore, wScore, mScore;
     for (int i = 0; i < 30; ++i)
     {
          rScore = rand.Next(400, 800);
          wScore = rand.Next(400, 800);
          mScore = rand.Next(400, 800);
          dt.Rows.Add(
               string.Format("S{0:000000}", rand.NextDouble() * 1000000),
               rScore, wScore, mScore, rScore + wScore + mScore
               );
     }

     DataTable dtSorted = dt.Clone();
     foreach (DataRow row in dt.Select("", "StudentID asc"))
          dtSorted.ImportRow(row);

     return dtSorted;
}

The formulas are evaluated by WordTemplate, as you can see in the output file: