//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Word;
using DocumentFormat.OpenXml.Wordprocessing;
using System.Globalization;
namespace DsWordWeb.Demos
{
// This example demonstrates the available aggregate functions that can be used with the 'calc'
// report templates feature for data aggregation.
// Unlike other report templates constructs, aggregates do not cause repetition of content
// in the generated document. Instead, they aggregate data and insert the single resulting
// value into the document.
// Aggregate functions' arguments can be expressions involving data, other functions and constants.
public class DataTplAggregates
{
public GcWordDocument CreateDocx()
{
// Generate a simple data source with random numeric data:
var values = new List<decimal>();
var rnd = Util.NewRandom();
var count = rnd.Next(10, 20);
for (int i = 0; i < count; ++i)
values.Add(new decimal(Math.Round((double)rnd.Next(0, 1000000) / rnd.Next(1, 100), 2)));
var doc = new GcWordDocument();
doc.Body.Sections.First.PageSetup.Margin.Top = 36;
doc.Body.Sections.First.PageSetup.Margin.Bottom = 36;
doc.Body.Sections.First.PageSetup.Margin.Left = 36;
doc.Body.Sections.First.PageSetup.Margin.Right = 36;
// Add the data source:
doc.DataTemplate.DataSources.Add("ds", values);
// Styles and templates to show results:
var numberedListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.NumberDefault, "numberedListTemplate");
var bulletListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "bulletListTemplate");
var exStyle = doc.Styles[BuiltInStyleId.Heading3];
var resStyle = doc.Styles[BuiltInStyleId.Strong];
// Print data to be summarized:
var paras = doc.Body.Paragraphs;
paras.Add("Data values to aggregate:", doc.Styles[BuiltInStyleId.Heading2]);
var p = paras.Add("{{#ds}}{{ds.value}}{{/ds}}", doc.Styles[BuiltInStyleId.ListParagraph]);
p.ListFormat.Template = numberedListTemplate;
// Print the results:
paras.Add("Simple aggregates:", doc.Styles[BuiltInStyleId.Heading2]);
add("{{ calc Average(ds.value) }}");
add("{{ calc Count(ds.value) }}");
add("{{ calc First(ds.value) }}");
add("{{ calc Last(ds.value) }}");
add("{{ calc Max(ds.value) }}");
add("{{ calc Min(ds.value) }}");
add("{{ calc Sum(ds.value) }}");
paras.Add("Aggregates on expressions:", doc.Styles[BuiltInStyleId.Heading2]);
add("{{ calc Sum(ds.value / Count(ds.value)) }}");
add("{{ calc Average(Pow(ds.value, 2)) }}");
add("{{ calc Average(Sqrt(ds.value)) }}");
add("{{ calc Sum(Iif(ds.value > 50, ds.value, 0)) }}");
// Process the templates:
doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
// Add a short note describing the demo at the top of the document:
paras.Insert(
"This example demonstrates the available aggregate functions that can be used with the 'calc' " +
"report templates feature for data aggregation. " +
"Unlike other report templates constructs, aggregates do not cause repetition of content " +
"in the generated document. Instead, they aggregate data and insert the (single) resulting " +
"value into the document. " +
"For example, to insert the sum of a field's values in all records of a data source, " +
"the template '{{calc Sum(ds.value)}}' can be used. " +
"Aggregate functions' arguments can be expressions involving data, other functions and constants. " +
"The data source used in this demo is a list of random decimal values. " +
"Please see this example's source code for full details.",
InsertLocation.Start);
paras.Insert("Report templates: calc aggregate functions", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start);
// Done:
return doc;
void add(string expr)
{
// \x200B is a zero-width space used to prevent template expansion:
paras.Add(expr.Insert(1, "\x200B") + " : ", exStyle).ListFormat.Template = bulletListTemplate;
paras.Last.GetRange().Runs.Add(expr, resStyle);
}
}
}
}