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.
C# |
Copy Code |
---|---|
{ 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:
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.