Report Templates allow you to bind the template with JSON data source by using Add method of IDictionary interface. The data can be added from a JSON file, stream, string etc. and bound to populate actual data upon template processing. For more information on different ways to work with JSON data source, refer Data Source Binding.
The following example takes you through a step by step process to generate a Word document with the list of oceans provided as a JSON string.
C# |
Copy Code |
---|---|
GcWordDocument doc = new GcWordDocument(); //Load template layout doc.Load("OceansList[Template].docx"); |
C# |
Copy Code |
---|---|
var oceans = @"[ { ""name"": ""Pacific"", ""areaOfWorldOcean"": 0.466, ""volumeOfWorldOcean"": 0.501, ""seas"": [ {""name"": ""Australasian Mediterranean Sea""}, {""name"": ""Philippine Sea""}, {""name"": ""Coral Sea""}, {""name"": ""South China Sea""}], }, { ""name"": ""Atlantic"", ""areaOfWorldOcean"": 0.235, ""volumeOfWorldOcean"": 0.233, ""seas"": [ {""name"": ""Sargasso Sea""}, {""name"": ""Caribbean Sea""}, {""name"": ""Mediterranean Sea""}, {""name"": ""Gulf of Guinea""}], }, { ""name"": ""Indian"", ""areaOfWorldOcean"": 0.195, ""volumeOfWorldOcean"": 0.198, ""seas"": [ {""name"": ""Arabian Sea""}, {""name"": ""Bay of Bengal""}, {""name"": ""Andaman Sea""}, {""name"": ""Laccadive Sea""}], }, { ""name"": ""Southern"", ""areaOfWorldOcean"": 0.061, ""volumeOfWorldOcean"": 0.054, ""seas"": [ {""name"": ""Weddell Sea""}, {""name"": ""Somov Sea""}, {""name"": ""Riiser-Larsen Sea""}, {""name"": ""Lazarev Sea""}], }, { ""name"": ""Arctic"", ""areaOfWorldOcean"": 0.043, ""volumeOfWorldOcean"": 0.014, ""seas"": [ {""name"": ""Barents Sea""}, {""name"": ""Greenland Sea""}, {""name"": ""East Siberian Sea""}, {""name"": ""Kara Sea""}], } ]"; |
C# |
Copy Code |
---|---|
doc.DataTemplate.DataSources.Add("ds", oceans); |
C# |
Copy Code |
---|---|
//Process the template
doc.DataTemplate.Process(); |
C# |
Copy Code |
---|---|
//Save the final document doc.Save("List of Oceans.docx"); |