DataTplSimpleList.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 print a simple list of items
  14. '' from a hierarchical data source, uppercasing each item.
  15. Public Class DataTplSimpleList
  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 list template so that the data Is formatted as a list:
  39. Dim myListTemplate = doc.ListTemplates.Add(BuiltInListTemplateId.BulletDefault, "myListTemplate")
  40.  
  41. '' The single paragraph added to the document contains template tags
  42. '' - {{#ds}}..{{/ds}} -- root template, 'ds' is the name of the data source
  43. '' - {ds.seas.name} -- fetches the sea name
  44. '' - :toupper() -- uppercases the data:
  45. Dim p = doc.Body.Paragraphs.Add("{{#ds}}{{ds.seas.name}:toupper()}{{/ds}}", doc.Styles(BuiltInStyleId.ListParagraph))
  46. p.ListFormat.Template = myListTemplate
  47.  
  48. '' This call expands all data templates in the document,
  49. '' replacing template tags with data (iterating over all data items):
  50. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"))
  51.  
  52. '' Done
  53. Return doc
  54. End Function
  55. End Class
  56.