DataTplCultureOut.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 System.Xml;
  12. using GrapeCity.Documents.Word;
  13.  
  14. namespace DsWordWeb.Demos
  15. {
  16. // This example shows how to format data from different data sources
  17. // in a culture-aware way by associating the correct culture with
  18. // each data source.
  19. public class DataTplCultureOut
  20. {
  21. public GcWordDocument CreateDocx()
  22. {
  23. // USD to EUR conversion rate:
  24. if (ExchangeOperations.ExchangeRateToEuro.TryGetValue("USD", out decimal usd2eur))
  25. {
  26. // Define the 'dsEn' data source to contain random amounts in US dollars:
  27. var dsEn = new List<decimal>();
  28. var rnd = Util.NewRandom();
  29. for (int i = 0; i < rnd.Next(5, 15); ++i)
  30. dsEn.Add((decimal)(rnd.Next(10, 1000) / 10f));
  31.  
  32. // The 'dsFr' data source will contain same amounts converted to EUR:
  33. var dsFr = new List<decimal>();
  34. foreach (var d in dsEn)
  35. dsFr.Add(d / usd2eur);
  36.  
  37. // Create Word document
  38. var doc = new GcWordDocument();
  39.  
  40. // Add the two data sources, associating a proper culture with each data source:
  41. doc.DataTemplate.DataSources.Add("dsEn", dsEn, CultureInfo.GetCultureInfo("en-US"));
  42. doc.DataTemplate.DataSources.Add("dsFr", dsFr, CultureInfo.GetCultureInfo("fr-FR"));
  43.  
  44. // The template will print two columns of data, the amounts from 'dsEn' in the first column,
  45. // the corresponding amounts from 'dsFr' in the second one. Because we specified correct
  46. // cultures for the two data sources, the culture-aware currency format will format
  47. // the data appropriately:
  48. doc.Body.Paragraphs.Add("{{#dsEn}:seq(x)}{{#dsFr}:follow(x)}{{dsEn.value}:format(C)} is approximately {{dsFr.value}:format(C)}{{/dsFr}}{{/dsEn}}");
  49.  
  50. // Process the template:
  51. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  52.  
  53. // Done:
  54. return doc;
  55. }
  56. else
  57. {
  58. var doc = new GcWordDocument();
  59. doc.Body.Paragraphs.Add("Could not get exchange rates from www.ecb.int, please try again later.", doc.Styles[BuiltInStyleId.Heading1]);
  60. return doc;
  61. }
  62. }
  63.  
  64. // From https://docs.microsoft.com/en-us/answers/questions/785478/api-for-currency-exchange-rate.html:
  65. internal static class ExchangeOperations
  66. {
  67. public static readonly Dictionary<string, decimal> ExchangeRateToEuro = new Dictionary<string, decimal>();
  68. public static List<string> FromCurrency = new List<string>();
  69. public static List<string> ToCurrency = new List<string>();
  70.  
  71. static ExchangeOperations()
  72. {
  73. LoadRates();
  74. }
  75.  
  76. public static void LoadRates()
  77. {
  78. try
  79. {
  80. XmlDocument xmlDoc = new XmlDocument();
  81. xmlDoc.Load("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
  82.  
  83. foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes[2].ChildNodes[0].ChildNodes)
  84. {
  85. ExchangeRateToEuro.Add(node.Attributes["currency"].Value, decimal.Parse(node.Attributes["rate"].Value));
  86. FromCurrency.Add(node.Attributes["currency"].Value);
  87. ToCurrency.Add(node.Attributes["currency"].Value);
  88. }
  89. }
  90. catch
  91. {
  92. // Eat errors, ExchangeRateToEuro will stay empty.
  93. }
  94. }
  95. }
  96. }
  97. }
  98.