//// This code is part of Document Solutions for Word demos.// Copyright (c) MESCIUS inc. All rights reserved.//usingSystem;usingSystem.IO;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Linq;usingSystem.Globalization;usingGrapeCity.Documents.Word; namespaceDsWordWeb.Demos{// This data template sample prints an invoice containing the list// of products in the purchase order.// The invoice data is generated for a randomly selected// order from the sample DsNWind database.publicclassDataTplOrderInvoice {publicGcWordDocumentCreateDocx(int _ = 0) {usingvar ds = newDataSet();// Load the sample database, fetch a random supplier// and a random order from that supplier: ds.ReadXml(Path.Combine("Resources", "data", "DsNWind.xml")); // Database tables used by the invoice:var dtSuppliers = ds.Tables["Suppliers"];var dtOrders = ds.Tables["OrdersCustomersEmployees"];var dtOrdersDetails = ds.Tables["EmployeesProductsOrders"]; // Collect order data:var random = Util.NewRandom(); var fetchedIndex = random.Next(dtSuppliers.Select().Count());var supplier = dtSuppliers.Select() .Skip(fetchedIndex).Take(1) .Select(it => new {SupplierID = Convert.ToInt32(it["SupplierID"]),CompanyName = it["CompanyName"].ToString(),ContactName = it["ContactName"].ToString(),ContactTitle = it["ContactTitle"].ToString(),Address = it["Address"].ToString(),City = it["City"].ToString(),Region = it["Region"].ToString(),PostalCode = it["PostalCode"].ToString(),Country = it["Country"].ToString(),Phone = it["Phone"].ToString(),Fax = it["Fax"].ToString(),HomePage = it["HomePage"].ToString() }).FirstOrDefault(); fetchedIndex = random.Next(dtOrders.Select().Count());var order = dtOrders.Select() .Skip(fetchedIndex).Take(1) .Select(it => new {OrderDate = it["OrderDate"],OrderID = Convert.ToInt32(it["OrderID"]),CompanyName = it["CompanyName"].ToString(),Name = $"{it["FirstName"]}{it["LastName"]}",Address = $"{it["ShipAddress"]},\n{it["ShipCity"]}{it["ShipRegion"]}{it["ShipPostalCode"]}{it["ShipCountry"]}",Email = GetEmail(it["FirstName"].ToString(), it["LastName"].ToString(), it["CompanyName"].ToString()), }).FirstOrDefault(); var orderDetails = dtOrdersDetails.Select() .Select(it => new {OrderID = Convert.ToInt32(it["OrderID"]),ProductName = it["ProductName"].ToString(),UnitPrice = Convert.ToDecimal(it["UnitPrice"]),Quantity = Convert.ToDecimal(it["Quantity"]),Total = Convert.ToDecimal(it["UnitPrice"]) * Convert.ToDecimal(it["Quantity"]), }) .Where(it => it.OrderID == order.OrderID) .OrderBy(it => it.ProductName).ToList(); // Finally, prep the integrated data source for the template:var data = new {o = order,ps = orderDetails,total = orderDetails.Sum(od_ => od_.Total), }; // Load the template DOCX, add the data source and process the template:var doc = newGcWordDocument(); doc.Load(Path.Combine("Resources", "WordDocs", "InvoiceTemplate.docx")); doc.DataTemplate.DataSources.Add("ds", data); doc.DataTemplate.Process(CultureInfo.GetCultureInfo("en-US")); // Done:return doc; } // Generate a sample email address from other data:privatestringGetEmail(string firstName, string lastName, string companyName) {var x = newstring(companyName.ToLower().Where(c_ => char.IsLetterOrDigit(c_)).ToArray());return$"{firstName.ToLower()}.{char.ToLower(lastName[0])}@{x}.com"; } publicstaticList<string[]> GetSampleParamsList() {returnnewList<string[]>() {newstring[] { "@data-templates/Order Invoice", "Generate the invoice for a random order fetched from a database", null },newstring[] { "@use-data-tpl/Order Invoice", "Generate the invoice for a random order fetched from a database", null }, }; } }}