DataTplNestedTable.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 GrapeCity.Documents.Word;
  12.  
  13. namespace DsWordWeb.Demos
  14. {
  15. // This sample uses data templates to generate a table with rows
  16. // corresponding to oceans, and a nested table with seas.
  17. public class DataTplNestedTable
  18. {
  19. public GcWordDocument CreateDocx()
  20. {
  21. // The data source (ocean and sea data from Wikipedia):
  22. var oceans = new[]
  23. {
  24. new { name = "Pacific", areaOfWorldOcean = 0.466, volumeOfWorldOcean = 0.501, seas = new[]
  25. { new {name = "Australasian Mediterranean Sea" }, new { name = "Philippine Sea" }, new { name = "Coral Sea" }, new { name = "South China Sea"} }
  26. },
  27. new { name = "Atlantic", areaOfWorldOcean = 0.235, volumeOfWorldOcean = 0.233, seas = new[]
  28. { new {name = "Sargasso Sea" }, new { name = "Caribbean Sea" }, new { name = "Mediterranean Sea" }, new { name = "Gulf of Guinea" } }
  29. },
  30. new { name = "Indian", areaOfWorldOcean = 0.195, volumeOfWorldOcean = 0.198, seas = new[]
  31. { new {name = "Arabian Sea" }, new { name = "Bay of Bengal" }, new { name = "Andaman Sea" }, new { name = "Laccadive Sea" } }
  32. },
  33. new { name = "Southern", areaOfWorldOcean = 0.061, volumeOfWorldOcean = 0.054, seas = new[]
  34. { new {name = "Weddell Sea" }, new { name = "Somov Sea" }, new { name = "Riiser-Larsen Sea" }, new { name = "Lazarev Sea" } }
  35. },
  36. new { name = "Arctic", areaOfWorldOcean = 0.043, volumeOfWorldOcean = 0.014, seas = new[]
  37. { new {name = "Barents Sea" }, new { name = "Greenland Sea" }, new { name = "East Siberian Sea" }, new { name = "Kara Sea" } }
  38. },
  39. };
  40.  
  41. var doc = new GcWordDocument();
  42.  
  43. // Add the data source to the data template data sources:
  44. doc.DataTemplate.DataSources.Add("ds", oceans);
  45.  
  46. // Add a table for oceans, and a nested table for seas:
  47. var t = doc.Body.Tables.Add(3, 1);
  48. t.Style = doc.Styles.Add("my table style", StyleType.Table);
  49. t.Style.BaseStyle = doc.Styles[BuiltInStyleId.ListTable5DarkAccent1];
  50. var tnested = t.Rows[0].Cells[2].GetRange().Tables.Add(1, 1);
  51. tnested.Style = doc.Styles.Add("my nested table style", StyleType.Table);
  52. tnested.Style.BaseStyle = t.Style.BaseStyle;
  53. tnested.Style.Font.Color.RGB = Color.PaleGoldenrod;
  54. tnested.Style.Font.Size -= 1;
  55.  
  56. // Specify data bindings:
  57. t.Rows[0].Cells[0].GetRange().Paragraphs.First.GetRange().Runs.Add("{{#ds}}{{name}}");
  58. t.Rows[0].Cells[1].GetRange().Paragraphs.First.GetRange().Runs.Add("{{areaOfWorldOcean}:format(0.#%)}");
  59. tnested.Rows[0].Cells[0].GetRange().Paragraphs.First.GetRange().Runs.Add("{{seas.name}}");
  60. t.Rows[0].Cells[2].GetRange().Paragraphs.First.GetRange().Runs.Add("{{/ds}}");
  61.  
  62. // This call expands all data templates in the document,
  63. // replacing template tags with data (iterating over all data items):
  64. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"));
  65.  
  66. // Done:
  67. return doc;
  68. }
  69. }
  70. }
  71.