DataTplAggregates.cs
  1. //
  2. // This code is part of Document Solutions for Word demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using GrapeCity.Documents.Word;
  11. using DocumentFormat.OpenXml.Wordprocessing;
  12. using System.Globalization;
  13.  
  14. namespace DsWordWeb.Demos
  15. {
  16. // This example demonstrates the available aggregate functions that can be used with the 'calc'
  17. // report templates feature for data aggregation.
  18. // Unlike other report templates constructs, aggregates do not cause repetition of content
  19. // in the generated document. Instead, they aggregate data and insert the single resulting
  20. // value into the document.
  21. // Aggregate functions' arguments can be expressions involving data, other functions and constants.
  22. public class DataTplAggregates
  23. {
  24. public GcWordDocument CreateDocx()
  25. {
  26. // Generate a simple data source with random numeric data:
  27. var values = new List<decimal>();
  28. var rnd = Util.NewRandom();
  29. var count = rnd.Next(10, 20);
  30. for (int i = 0; i < count; ++i)
  31. values.Add(new decimal(Math.Round((double)rnd.Next(0, 1000000) / rnd.Next(1, 100), 2)));
  32.  
  33. var doc = new GcWordDocument();
  34. doc.Body.Sections.First.PageSetup.Margin.Top = 36;
  35. doc.Body.Sections.First.PageSetup.Margin.Bottom = 36;
  36. doc.Body.Sections.First.PageSetup.Margin.Left = 36;
  37. doc.Body.Sections.First.PageSetup.Margin.Right = 36;
  38.  
  39. // Add the data source:
  40. doc.DataTemplate.DataSources.Add("ds", values);
  41.  
  42. // Styles and templates to show results:
  43. var numberedListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.NumberDefault, "numberedListTemplate");
  44. var bulletListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "bulletListTemplate");
  45. var exStyle = doc.Styles[BuiltInStyleId.Heading3];
  46. var resStyle = doc.Styles[BuiltInStyleId.Strong];
  47.  
  48. // Print data to be summarized:
  49. var paras = doc.Body.Paragraphs;
  50. paras.Add("Data values to aggregate:", doc.Styles[BuiltInStyleId.Heading2]);
  51. var p = paras.Add("{{#ds}}{{ds.value}}{{/ds}}", doc.Styles[BuiltInStyleId.ListParagraph]);
  52. p.ListFormat.Template = numberedListTemplate;
  53.  
  54. // Print the results:
  55. paras.Add("Simple aggregates:", doc.Styles[BuiltInStyleId.Heading2]);
  56. add("{{ calc Average(ds.value) }}");
  57. add("{{ calc Count(ds.value) }}");
  58. add("{{ calc First(ds.value) }}");
  59. add("{{ calc Last(ds.value) }}");
  60. add("{{ calc Max(ds.value) }}");
  61. add("{{ calc Min(ds.value) }}");
  62. add("{{ calc Sum(ds.value) }}");
  63.  
  64. paras.Add("Aggregates on expressions:", doc.Styles[BuiltInStyleId.Heading2]);
  65. add("{{ calc Sum(ds.value / Count(ds.value)) }}");
  66. add("{{ calc Average(Pow(ds.value, 2)) }}");
  67. add("{{ calc Average(Sqrt(ds.value)) }}");
  68. add("{{ calc Sum(Iif(ds.value > 50, ds.value, 0)) }}");
  69.  
  70. // Process the templates:
  71. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  72.  
  73. // Add a short note describing the demo at the top of the document:
  74. paras.Insert(
  75. "This example demonstrates the available aggregate functions that can be used with the 'calc' " +
  76. "report templates feature for data aggregation. " +
  77. "Unlike other report templates constructs, aggregates do not cause repetition of content " +
  78. "in the generated document. Instead, they aggregate data and insert the (single) resulting " +
  79. "value into the document. " +
  80. "For example, to insert the sum of a field's values in all records of a data source, " +
  81. "the template '{{calc Sum(ds.value)}}' can be used. " +
  82. "Aggregate functions' arguments can be expressions involving data, other functions and constants. " +
  83. "The data source used in this demo is a list of random decimal values. " +
  84. "Please see this example's source code for full details.",
  85. InsertLocation.Start);
  86. paras.Insert("Report templates: calc aggregate functions", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start);
  87.  
  88. // Done:
  89. return doc;
  90.  
  91. void add(string expr)
  92. {
  93. // \x200B is a zero-width space used to prevent template expansion:
  94. paras.Add(expr.Insert(1, "​​​\x200B") + " : ", exStyle).ListFormat.Template = bulletListTemplate;
  95. paras.Last.GetRange().Runs.Add(expr, resStyle);
  96. }
  97. }
  98. }
  99. }
  100.