DataTplProductList.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 data template sample loads a DOCX that was created in MS Word,
  14. '' And contains a table with data template tags. It then creates a DataSet,
  15. '' loads the GcNwind.xml data base into it, And builds a product list
  16. '' from tables in that data set. The product list Is set as the template
  17. '' data source on the document, And the template Is processed to build
  18. '' the final document with actual data.
  19. Public Class DataTplProductList
  20. Public Function CreateDocx(ByRef sampleParams As String()) As GcWordDocument
  21. Dim doc = New GcWordDocument()
  22.  
  23. '' Load the template DOCX
  24. doc.Load(Path.Combine("Resources", "WordDocs", sampleParams(3)))
  25.  
  26. Using ds = New DataSet()
  27. '' Load the data And build the product list data source:
  28. ds.ReadXml(Path.Combine("Resources", "data", "GcNWind.xml"))
  29.  
  30. Dim dtProds As DataTable = ds.Tables("Products")
  31. Dim dtSupps As DataTable = ds.Tables("Suppliers")
  32.  
  33. Dim products =
  34. From prod In dtProds.Select()
  35. Join supp In dtSupps.Select()
  36. On prod("SupplierID") Equals supp("SupplierID")
  37. Order By prod("ProductName")
  38. Select New With
  39. {
  40. .ProductID = prod("ProductID"),
  41. .ProductName = prod("ProductName"),
  42. .Supplier = supp("CompanyName"),
  43. .QuantityPerUnit = prod("QuantityPerUnit"),
  44. .UnitPrice = prod("UnitPrice")
  45. }
  46.  
  47. '' Add the data source to the data template data sources
  48. '' (note that in this release, only one data source can be added):
  49. doc.DataTemplate.DataSources.Add("ds", products)
  50.  
  51. '' The document already has all the necessary bindings,
  52. '' so we only need to process the data template:
  53. doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"))
  54. End Using
  55.  
  56. '' Done
  57. Return doc
  58. End Function
  59.  
  60. Public Function CreateDocx(Optional parsIdx As Integer = 0) As GcWordDocument
  61. Return CreateDocx(GetSampleParamsList()(parsIdx))
  62. End Function
  63.  
  64. Public Shared Function GetSampleParamsList() As List(Of String())
  65. '' Strings are name, description, info, template docx
  66. Return New List(Of String()) From
  67. {
  68. New String() {"@data-templates/Product List",
  69. "Load a template DOCX and bind it to a product list from an XML data set", Nothing,
  70. "ProductListTemplate.docx"},
  71. New String() {"@data-templates/Formatter Chain",
  72. "Load a template that uses chained formatters to highlight products that cost $50+", Nothing,
  73. "ProductListTemplate-cond.docx"}
  74. }
  75. End Function
  76. End Class
  77.