DataTplTopProduct.cs
- //
- // This code is part of Document Solutions for Word demos.
- // Copyright (c) MESCIUS inc. All rights reserved.
- //
- using System;
- using System.IO;
- using System.Drawing;
- using System.Collections.Generic;
- using System.Text;
- using System.Data;
- using System.Linq;
- using System.Globalization;
- using GrapeCity.Documents.Word;
-
- namespace DsWordWeb.Demos
- {
- // 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
- {
- public GcWordDocument CreateDocx()
- {
- // The number of iterations:
- const int N = 10;
-
- // Load and modify the template DOCX:
- var doc = new GcWordDocument();
- doc.Load(Path.Combine("Resources", "WordDocs", "ProductListTemplate.docx"));
- var caption0 = "Product List";
- var caption1 = $"{N} Most Expensive Products";
- var pStart0 = @"{{#ds}}{{ds.ProductID}}";
- var pEnd0 = @"{{/ds}}";
- var pStart1 = @"{{#take}:seq(t10)}{{#ds}:follow(t10)}{{ds.ProductID}}";
- var 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 int[N]);
-
- using var ds = new DataSet();
- // Load the data and build the product list data source:
- ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.xml"));
-
- DataTable dtProds = ds.Tables["Products"];
- DataTable dtSupps = ds.Tables["Suppliers"];
-
- var products =
- from prod in dtProds.Select()
- join supp in dtSupps.Select()
- on prod["SupplierID"] equals supp["SupplierID"]
- orderby prod["UnitPrice"] descending
- select new
- {
- 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;
- }
- }
- }
-