DataTplIfStartsWith.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 System.Globalization;
  11. using GrapeCity.Documents.Word;
  12.  
  13. namespace DsWordWeb.Demos
  14. {
  15. // This example demonstrates the use of 'if/else' construct in report templates
  16. // to filter data source on a condition (that a certain field starts with the letter 'S').
  17. public class DataTplIfStartsWith
  18. {
  19. public GcWordDocument CreateDocx()
  20. {
  21. // The data source (ocean and sea data from Wikipedia):
  22. var oceans = new[]
  23. {
  24. new { name = "Pacific", areaOfWorldOcean = 0.466, volumeOfWorldOcean = 0.501, seas = new[]
  25. { new {name = "Australasian Mediterranean Sea" }, new { name = "Philippine Sea" }, new { name = "Coral Sea" }, new { name = "South China Sea"} }
  26. },
  27. new { name = "Atlantic", areaOfWorldOcean = 0.235, volumeOfWorldOcean = 0.233, seas = new[]
  28. { new {name = "Sargasso Sea" }, new { name = "Caribbean Sea" }, new { name = "Mediterranean Sea" }, new { name = "Gulf of Guinea" } }
  29. },
  30. new { name = "Indian", areaOfWorldOcean = 0.195, volumeOfWorldOcean = 0.198, seas = new[]
  31. { new {name = "Arabian Sea" }, new { name = "Bay of Bengal" }, new { name = "Andaman Sea" }, new { name = "Laccadive Sea" } }
  32. },
  33. new { name = "Southern", areaOfWorldOcean = 0.061, volumeOfWorldOcean = 0.054, seas = new[]
  34. { new {name = "Weddell Sea" }, new { name = "Somov Sea" }, new { name = "Riiser-Larsen Sea" }, new { name = "Lazarev Sea" } }
  35. },
  36. new { name = "Arctic", areaOfWorldOcean = 0.043, volumeOfWorldOcean = 0.014, seas = new[]
  37. { new {name = "Barents Sea" }, new { name = "Greenland Sea" }, new { name = "East Siberian Sea" }, new { name = "Kara Sea" } }
  38. },
  39. };
  40.  
  41. var doc = new GcWordDocument();
  42.  
  43. // Add the data source:
  44. doc.DataTemplate.DataSources.Add("ds", oceans);
  45.  
  46. // Add a list template so that the data is formatted as a list:
  47. var myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");
  48.  
  49. // Filter output using if/then/else - print only names starting with an 's':
  50. var p = doc.Body.Paragraphs.Add("{{if StartsWith(UCase(ds.seas.name),\"S\")}}{{ds.seas.name}}{{endif}}", doc.Styles[BuiltInStyleId.ListParagraph]);
  51. p.ListFormat.Template = myListTemplate;
  52.  
  53. // Expand the templates:
  54. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  55.  
  56. // Add a short note describing the demo at the top of the document:
  57. doc.Body.Paragraphs.Insert(
  58. "This example demonstrates the use of 'if/else' construct in report templates " +
  59. "to filter data source on a condition (that a certain field starts with the letter 'S')." +
  60. "Please see this sample source code for full details.",
  61. InsertLocation.Start);
  62. doc.Body.Paragraphs.Insert("Report templates: if data starts with 'S'", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start);
  63.  
  64. // Done:
  65. return doc;
  66. }
  67. }
  68. }
  69.