[]
        
(Showing Draft Content)

Generate Document using Multiple Data Sources

You can use multiple data sources with Report Templates to generate Word documents upon template processing. The following example demonstrates how to generate a Word document with the list of car brands followed by the list of car body styles by using two data sources.

  1. Define data in two JSON datasources, containing:

    • A list of some popular car makers

    • A list of various car body styles

  2. Create a new Word document by instantiating the GcWordDocument class.

  3. Add both datasources with specified data by using Add method of the IDictionary interface.

  4. Add template tags in a paragraph by using Add method of the ParagraphCollection class.

  5. Process the template by using Process method and save the final Word document.

    {
     var makes = "[" +
         "{ \"make\": \"Toyota\" }," +
         "{ \"make\": \"General Motors\" }," +
         "{ \"make\": \"Volkswagen\" }," +
         "{ \"make\": \"Ford\" }," +
         "{ \"make\": \"BMW\" }," +
         "{ \"make\": \"Nissan\" }," +
         "{ \"make\": \"Hyundai\" }," +
         "{ \"make\": \"Honda\" }," +
         "{ \"make\": \"Mazda\" }," +
         "{ \"make\": \"Jaguar\" }," +
         "]";
     var bodyStyles = "[" +
         "{ \"style\": \"Sedan\" }," +
         "{ \"style\": \"Coupe\" }," +
         "{ \"style\": \"Hatchback\" }," +
         "{ \"style\": \"SUV\" }," +
         "{ \"style\": \"Crossover\" }," +
         "{ \"style\": \"Minivan\" }," +
         "{ \"style\": \"Pickup\" }," +
         "{ \"style\": \"Wagon\" }," +
         "]";
    
     var doc = new GcWordDocument();
    
     // Add the data sources
     doc.DataTemplate.DataSources.Add("makes", makes);
     doc.DataTemplate.DataSources.Add("styles", bodyStyles);
    
     // Print the list of some car makes
     doc.Body.Paragraphs.Add("Popular Car Makers", doc.Styles[BuiltInStyleId.Heading2]);
     doc.Body.Paragraphs.Add("{{#makes}}{{makes.make}}{{/makes}}", doc.Styles[BuiltInStyleId.ListBullet]);
    
     // Print the list of car body styles
     doc.Body.Paragraphs.Add("Car Body Styles", doc.Styles[BuiltInStyleId.Heading2]);
     doc.Body.Paragraphs.Add("{{#styles}}{{styles.style}}{{/styles}}", doc.Styles[BuiltInStyleId.ListBullet]);
    
     doc.DataTemplate.Process();
     doc.Save("MultipleDataSources.docx");

The output of above code will look like below in the Word document:

Multiple data sources used in GcWord Report Templates

Limitation

While using different datasources, there may be identical paths (like {{ds1.employee.name}} and {{ds2.employee.name}}). Hence, the fully qualified template tag should be used which includes the name of datasource. The unqualified tags (like {{employee.name}}) will throw an exception.