''
'' 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 example is similar to DataTplProductList, but additionally
'' the code uses report templates' conditional construct '{{if...}}..{{else}}..{{endif}}'
'' to filter data so that only products with price greater than $50 are included.
'' Note that the filtered data is rendered as a table, each record in a separate table row.
'' If the first column of a table starts with '{{if...}}' and ends with '{{endif}}',
'' empty rows added as the result of template expansion will be removed.
Public Class DataTplProductIf
Function CreateDocx() As GcWordDocument
'' Load and modify the template DOCX:
Dim doc = New GcWordDocument()
doc.Load(Path.Combine("Resources", "WordDocs", "ProductListTemplate.docx"))
Dim caption0 = "Product List"
Dim caption1 = $"Products that cost more than $50"
Dim pStart0 = "{{#ds}}{{ds.ProductID}}"
Dim pEnd0 = "{{/ds}}"
'' NOTE: for empty rows to be automatically removed, the first cell in the template row
'' must start with {{if ...}}, and the last cell must end with matching {{endif}}:
Dim pStart1 = "{{if ds.UnitPrice > 50}}{{ds.ProductID}}"
Dim pEnd1 = "{{endif}}"
doc.Body.Replace(caption0, caption1)
doc.Body.Replace(pEnd0, pEnd1)
doc.Body.Replace(pStart0, pStart1)
Using ds = New DataSet()
'' Load 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)
'' Process the template:
doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US"))
'' Done:
Return doc
End Using
End Function
End Class