DataTplNestedTable.vb
  1. ''
  2. '' This code is part of Document Solutions for Word demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Text
  8. Imports System.Data
  9. Imports System.Linq
  10. Imports System.Globalization
  11. Imports GrapeCity.Documents.Word
  12.  
  13. '' This sample uses data templates to generate a table with rows
  14. '' corresponding to oceans, and a nested table with seas.
  15. Public Class DataTplNestedTable
  16. Function CreateDocx() As GcWordDocument
  17. '' The data source (ocean And sea data from Wikipedia):
  18. Dim oceans =
  19. {
  20. New With {.name = "Pacific", .areaOfWorldOcean = 0.466, .volumeOfWorldOcean = 0.501, .seas =
  21. {New With {.name = "Australasian Mediterranean Sea"}, New With {.name = "Philippine Sea"}, New With {.name = "Coral Sea"}, New With {.name = "South China Sea"}}},
  22. New With {.name = "Atlantic", .areaOfWorldOcean = 0.235, .volumeOfWorldOcean = 0.233, .seas =
  23. {New With {.name = "Sargasso Sea"}, New With {.name = "Caribbean Sea"}, New With {.name = "Mediterranean Sea"}, New With {.name = "Gulf of Guinea"}}},
  24. New With {.name = "Indian", .areaOfWorldOcean = 0.195, .volumeOfWorldOcean = 0.198, .seas =
  25. {New With {.name = "Arabian Sea"}, New With {.name = "Bay of Bengal"}, New With {.name = "Andaman Sea"}, New With {.name = "Laccadive Sea"}}},
  26. New With {.name = "Southern", .areaOfWorldOcean = 0.061, .volumeOfWorldOcean = 0.054, .seas =
  27. {New With {.name = "Weddell Sea"}, New With {.name = "Somov Sea"}, New With {.name = "Riiser-Larsen Sea"}, New With {.name = "Lazarev Sea"}}},
  28. New With {.name = "Arctic", .areaOfWorldOcean = 0.043, .volumeOfWorldOcean = 0.014, .seas =
  29. {New With {.name = "Barents Sea"}, New With {.name = "Greenland Sea"}, New With {.name = "East Siberian Sea"}, New With {.name = "Kara Sea"}}}
  30. }
  31.  
  32. Dim doc = New GcWordDocument()
  33.  
  34. '' Add the data source to the data template data sources
  35. '' (note that in this release, only one data source can be added):
  36. doc.DataTemplate.DataSources.Add("ds", oceans)
  37.  
  38. '' Add a table for oceans, And a nested table for seas:
  39. Dim t = doc.Body.Tables.Add(3, 1)
  40. t.Style = doc.Styles.Add("my table style", StyleType.Table)
  41. t.Style.BaseStyle = doc.Styles(BuiltInStyleId.ListTable5DarkAccent1)
  42. Dim tnested = t.Rows(0).Cells(2).GetRange().Tables.Add(1, 1)
  43. tnested.Style = doc.Styles.Add("my nested table style", StyleType.Table)
  44. tnested.Style.BaseStyle = t.Style.BaseStyle
  45. tnested.Style.Font.Color.RGB = Color.PaleGoldenrod
  46. tnested.Style.Font.Size -= 1
  47.  
  48. '' Specify data bindings
  49. t.Rows(0).Cells(0).GetRange().Paragraphs.First.GetRange().Runs.Add("{{#ds}}{{name}}")
  50. t.Rows(0).Cells(1).GetRange().Paragraphs.First.GetRange().Runs.Add("{{areaOfWorldOcean}:format(0.#%)}")
  51. tnested.Rows(0).Cells(0).GetRange().Paragraphs.First.GetRange().Runs.Add("{{seas.name}}")
  52. t.Rows(0).Cells(2).GetRange().Paragraphs.First.GetRange().Runs.Add("{{/ds}}")
  53.  
  54. '' This call expands all data templates in the document,
  55. '' replacing template tags with data (iterating over all data items):
  56. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"))
  57.  
  58. '' Done
  59. Return doc
  60. End Function
  61. End Class
  62.