DataTplCalcFormat.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 'calc' Format function
  16. // with optional format string and locale arguments.
  17. public class DataTplCalcFormat
  18. {
  19. public GcWordDocument CreateDocx()
  20. {
  21. var dsDate = new DateTime[] { new DateTime(2023, 04, 12) };
  22. var dsNum = new double[] { 123.456 };
  23.  
  24. var doc = new GcWordDocument();
  25. var list = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate");
  26. var style = doc.Styles[BuiltInStyleId.ListParagraph];
  27.  
  28. doc.DataTemplate.DataSources.Add("dsDate", dsDate);
  29. doc.DataTemplate.DataSources.Add("dsNum", dsNum);
  30.  
  31. var paras = doc.Body.Paragraphs;
  32. var p = paras.Add("Date formatting examples:", style);
  33. p.ListFormat.Template = list;
  34. p = paras.Add("Date with default formatting: {{calc Format(dsDate.value)}}", style);
  35. p.ListFormat.Template = list;
  36. p.ListFormat.LevelNumber = 1;
  37. p = paras.Add("Date formatted as ('D', 'en-US'): {{calc Format(dsDate.value, \"D\", \"en-US\")}}", style);
  38. p.ListFormat.Template = list;
  39. p.ListFormat.LevelNumber = 1;
  40. p = paras.Add("Date formatted as ('D', 'fr-FR'): {{calc Format(dsDate.value, \"D\", \"fr-FR\")}}", style);
  41. p.ListFormat.Template = list;
  42. p.ListFormat.LevelNumber = 1;
  43. p = paras.Add("Date formatted as ('D', 'ja-JP'): {{calc Format(dsDate.value, \"D\", \"ja-JP\")}}", style);
  44. p.ListFormat.Template = list;
  45. p.ListFormat.LevelNumber = 1;
  46.  
  47. p = paras.Add("Number formatting examples:", style);
  48. p.ListFormat.Template = list;
  49. p = paras.Add("Number with default formatting: {{calc Format(dsNum.value)}}", style);
  50. p.ListFormat.Template = list;
  51. p.ListFormat.LevelNumber = 1;
  52. p = paras.Add("Number formatted as ('C', 'en-US'): {{calc Format(dsNum.value, \"C\", \"en-US\")}}", style);
  53. p.ListFormat.Template = list;
  54. p.ListFormat.LevelNumber = 1;
  55. p = paras.Add("Number formatted as ('C', 'fr-FR'): {{calc Format(dsNum.value, \"C\", \"fr-FR\")}}", style);
  56. p.ListFormat.Template = list;
  57. p.ListFormat.LevelNumber = 1;
  58. p = paras.Add("Number formatted as ('C3', 'ja-JP'): {{calc Format(dsNum.value, \"C3\", \"ja-JP\")}}", style);
  59. p.ListFormat.Template = list;
  60. p.ListFormat.LevelNumber = 1;
  61.  
  62. p = paras.Add("Expression formatting examples:", style);
  63. p.ListFormat.Template = list;
  64. p = paras.Add("Numeric expression: {{calc Format(dsNum.value / 2)}}", style);
  65. p.ListFormat.Template = list;
  66. p.ListFormat.LevelNumber = 1;
  67. p = paras.Add("Boolean expression: {{calc Format(IsFirst(dsNum.value))}}", style);
  68. p.ListFormat.Template = list;
  69. p.ListFormat.LevelNumber = 1;
  70.  
  71. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  72.  
  73. // Done:
  74. return doc;
  75. }
  76. }
  77. }
  78.