DataTplCalcDateTime.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 available date and time functions
  16. // that can be used with the 'calc' report templates feature.
  17. public class DataTplCalcDateTime
  18. {
  19. public GcWordDocument CreateDocx()
  20. {
  21. // Dummy data source (the date/time functions shown in this example do not need the data):
  22. var data = new int[] { 123 };
  23.  
  24. var doc = new GcWordDocument();
  25.  
  26. // Add the data source:
  27. doc.DataTemplate.DataSources.Add("ds", data);
  28.  
  29. // Styles and templates to show results:
  30. var bulletListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "bulletListTemplate");
  31. var exStyle = doc.Styles[BuiltInStyleId.Heading3];
  32. var resStyle = doc.Styles[BuiltInStyleId.Strong];
  33.  
  34. var paras = doc.Body.Paragraphs;
  35. add("{{ calc Now() }}");
  36. add("{{ calc Today() }}");
  37. add("{{ calc UtcNow() }}");
  38.  
  39. add("{{ calc AddTicks(Now(), 100) }}");
  40. add("{{ calc AddMilliSeconds(Now(), 60 * 1000) }}");
  41. add("{{ calc AddSeconds(Now(), 60) }}");
  42. add("{{ calc AddMinutes(Now(), 1) }}");
  43. add("{{ calc AddHours(Now(), 2) }}");
  44.  
  45. add("{{ calc AddDays(Today(), 7) }}");
  46. add("{{ calc AddMonths(Today(), 4) }}");
  47. add("{{ calc AddYears(Today(), 10) }}");
  48. // add("{{ calc AddTimeSpan(Today(), new TimeSpan(123)) }}");
  49.  
  50. add("{{ calc DateDiffTick(\"1-Jan-2001\", Today()) }}");
  51. add("{{ calc DateDiffMilliSecond(Today(), Now()) }}");
  52. add("{{ calc DateDiffSecond(Today(), Now()) }}");
  53. add("{{ calc DateDiffMinute(Today(), Now()) }}");
  54. add("{{ calc DateDiffHour(Today(), Now()) }}");
  55. add("{{ calc DateDiffDay(\"1-Jan-2001\", Today()) }}");
  56.  
  57. add("{{ calc GetTimeOfDay(Now()) }}");
  58. add("{{ calc GetMilliSecond(Now()) }}");
  59. add("{{ calc GetSecond(Now()) }}");
  60. add("{{ calc GetMinute(Now()) }}");
  61. add("{{ calc GetHour(Now()) }}");
  62.  
  63. add("{{ calc GetDate(Now()) }}");
  64. add("{{ calc GetDayOfWeek(Now()) }}");
  65. add("{{ calc GetDay(Now()) }}");
  66. add("{{ calc GetDayOfYear(Now()) }}");
  67. add("{{ calc GetMonth(Now()) }}");
  68. add("{{ calc GetYear(Now()) }}");
  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 date and time functions " +
  76. "that can be used with the 'calc' report templates feature. " +
  77. "Please see this example's source code for full details.",
  78. InsertLocation.Start);
  79. paras.Insert("Report templates: available calc date/time functions", doc.Styles[BuiltInStyleId.Heading1], InsertLocation.Start);
  80.  
  81. // Done:
  82. return doc;
  83.  
  84. void add(string expr)
  85. {
  86. // \x200B is a zero-width space used to prevent template expansion:
  87. paras.Add(expr.Insert(1, "​​​\x200B") + " : ", exStyle).ListFormat.Template = bulletListTemplate;
  88. paras.Last.GetRange().Runs.Add(expr, resStyle);
  89. }
  90. }
  91. }
  92. }
  93.