DataTplCultureOut.vb
''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Xml
Imports GrapeCity.Documents.Word

'' This example shows how to format data from different data sources
'' in a culture-aware way by associating the correct culture with
'' each data source.
Public Class DataTplCultureOut
    Function CreateDocx() As GcWordDocument
        '' USD to EUR conversion rate:
        Dim usd2eur As Decimal
        If ExchangeOperations.ExchangeRateToEuro.TryGetValue("USD", usd2eur) Then
            '' Define the 'dsEn' data source to contain random amounts in US dollars:
            Dim dsEn = New List(Of Decimal)
            Dim rnd = Util.NewRandom()
            Dim i = 0
            Do While i < rnd.Next(5, 15)
                dsEn.Add(CDec(rnd.Next(10, 1000) / 10F))
                i += 1
            Loop

            '' The 'dsFr' data source will contain same amounts converted to EUR:
            Dim dsFr = New List(Of Decimal)
            For Each d In dsEn
                dsFr.Add(d / usd2eur)
            Next

            '' Create Word document
            Dim doc = New GcWordDocument()

            '' Add the two data sources, associating a proper culture with each data source:
            doc.DataTemplate.DataSources.Add("dsEn", dsEn, CultureInfo.GetCultureInfo("en-US"))
            doc.DataTemplate.DataSources.Add("dsFr", dsFr, CultureInfo.GetCultureInfo("fr-FR"))

            '' The template will print two columns of data, the amounts from 'dsEn' in the first column,
            '' the corresponding amounts from 'dsFr' in the second one. Because we specified correct
            '' cultures for the two data sources, the culture-aware currency format will format
            '' the data appropriately:
            doc.Body.Paragraphs.Add("{{#dsEn}:seq(x)}{{#dsFr}:follow(x)}{{dsEn.value}:format(C)} is approximately {{dsFr.value}:format(C)}{{/dsFr}}{{/dsEn}}")

            '' Process the template:
            doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"))

            '' Done:
            Return doc
        Else
            Dim doc = New GcWordDocument()
            doc.Body.Paragraphs.Add("Could not get exchange rates from www.ecb.int, please try again later.", doc.Styles(BuiltInStyleId.Heading1))
            Return doc
        End If
    End Function

    '' From https://docs.microsoft.com/en-us/answers/questions/785478/api-for-currency-exchange-rate.html:
    Friend Class ExchangeOperations
        Public Shared ReadOnly ExchangeRateToEuro As New Dictionary(Of String, Decimal)
        Public Shared FromCurrency As New List(Of String)
        Public Shared ToCurrency As New List(Of String)

        Shared Sub New()
            LoadRates()
        End Sub

        Public Shared Sub LoadRates()
            Try
                Dim xmlDoc As New XmlDocument()
                xmlDoc.Load("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml")

                For Each node As XmlNode In xmlDoc.DocumentElement.ChildNodes(2).ChildNodes(0).ChildNodes
                    ExchangeRateToEuro.Add(node.Attributes("currency").Value, Decimal.Parse(node.Attributes("rate").Value))
                    FromCurrency.Add(node.Attributes("currency").Value)
                    ToCurrency.Add(node.Attributes("currency").Value)
                Next
            Catch
                '' Eat errors, ExchangeRateToEuro will stay empty.
            End Try
        End Sub
    End Class
End Class