''
'' This code is part of Document Solutions for Word demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.Data
Imports System.Globalization
Imports System.IO
Imports System.Linq
Imports GrapeCity.Documents.Word
'' This sample is similar to DataTplProductList but it sorts the product
'' list by price in the descending order to print 10 most expensive products.
'' To limit the list this sample uses the seq/follow formatter as a 'take(N)'
'' operator, utilizing also the ability to use multiple data sources.
'' The template document is modified to include the sequence/follow formatters.
Public Class DataTplTopProduct
Function CreateDocx() As GcWordDocument
'' The number of iterations:
Const N As Integer = 10
'' Load and modify the template DOCX:
Dim doc = New GcWordDocument()
doc.Load(Path.Combine("Resources", "WordDocs", "ProductListTemplate.docx"))
Dim caption0 = "Product List"
Dim caption1 = $"{N} Most Expensive Products"
Dim pStart0 = "{{#ds}}{{ds.ProductID}}"
Dim pEnd0 = "{{/ds}}"
Dim pStart1 = "{{#take}:seq(t10)}{{#ds}:follow(t10)}{{ds.ProductID}}"
Dim pEnd1 = "{{/ds}}{{/take}}"
doc.Body.Replace(caption0, caption1)
doc.Body.Replace(pEnd0, pEnd1)
doc.Body.Replace(pStart0, pStart1)
'' Add a simple array to be used as the sequence data source
'' limiting the number of iterations over the "real" data range:
doc.DataTemplate.DataSources.Add("take", New Integer(N - 1) {})
Using ds = New DataSet()
'' Load the data and build the product list data source:
ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.xml"))
Dim dtProds As DataTable = ds.Tables("Products")
Dim dtSupps As DataTable = ds.Tables("Suppliers")
Dim products =
From prod In dtProds.Select()
Join supp In dtSupps.Select()
On prod("SupplierID") Equals supp("SupplierID")
Order By prod("UnitPrice") Descending
Select New With {
.ProductID = prod("ProductID"),
.ProductName = prod("ProductName"),
.Supplier = supp("CompanyName"),
.QuantityPerUnit = prod("QuantityPerUnit"),
.UnitPrice = prod("UnitPrice")
}
'' Add the data source to the data template data sources:
doc.DataTemplate.DataSources.Add("ds", products)
'' The document already has all the necessary bindings,
'' so we only need to process the data template:
doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"))
'' Done:
Return doc
End Using
End Function
End Class