Example E4765
Visible to All Users

Word Processing Document API - How to Use Multiple Data Sources for a Mail Merge

This example illustrates how to use the Mail Merge functionality of the Word Processing File API to merge data from different data sources and merge resultant documents into one in an ASP.NET application. The approach illustrated in this example is similar to one utilized in the Office File API - Mail Merge online demo. The difference is that we use multiple data sources in our example, and all records from an associated data source are rendered in mail-merged document previews.

[!Important]
The Universal Subscription or an additional Office File API Subscription is required to use this example in production code. For pricing information, please refer to the DevExpress Subscription page.

Files to Look At

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

MailMergeSample/Default.aspx.cs(vb)
C#
using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Web.UI; using DevExpress.XtraRichEdit; using DevExpress.XtraRichEdit.API.Native; public partial class _Default : System.Web.UI.Page { RichEditDocumentServer documentServer = null; protected void Page_Load(object sender, EventArgs e) { documentServer = new RichEditDocumentServer(); if (Request.QueryString.Count > 0 && Request.QueryString[0].StartsWith("preview")) { RefreshPreview(Request.QueryString[0]); } } private void RefreshPreview(string previewName) { Response.StatusCode = (int)HttpStatusCode.OK; Response.ContentType = "text/html"; Stream outputStream = ExecuteMerge(previewName, DocumentFormat.Html); outputStream.Seek(0, SeekOrigin.Begin); StreamCopyHelper.Copy(outputStream, Response.OutputStream); Response.End(); } private Stream ExecuteMerge(string templateName, DocumentFormat documentFormat) { Stream result = new MemoryStream(); MailMergeOptions mailMergeOptions = documentServer.CreateMailMergeOptions(); if (templateName == "preview1") { documentServer.LoadDocument(Page.MapPath("~/App_Data/InvoicesDetail.rtf")); List<Invoice> invoices = new List<Invoice>(10); invoices.Add(new Invoice(0, "Invoice1", 10.0m)); invoices.Add(new Invoice(1, "Invoice2", 15.0m)); invoices.Add(new Invoice(2, "Invoice3", 20.0m)); mailMergeOptions.DataSource = invoices; } else if (templateName == "preview2") { documentServer.LoadDocument(Page.MapPath("~/App_Data/SamplesDetail.rtf")); mailMergeOptions.DataSource = ManualDataSet.CreateData().Tables[0]; } else if (templateName == "all") { Stream part1 = ExecuteMerge("preview1", documentFormat); Stream part2 = ExecuteMerge("preview2", documentFormat); part1.Seek(0, SeekOrigin.Begin); part2.Seek(0, SeekOrigin.Begin); documentServer.LoadDocument(part1, documentFormat); documentServer.Document.AppendDocumentContent(part2, documentFormat); documentServer.SaveDocument(result, documentFormat); return result; } documentServer.Options.Export.Html.EmbedImages = true; mailMergeOptions.MergeMode = MergeMode.JoinTables; documentServer.MailMerge(mailMergeOptions, result, documentFormat); return result; } protected void btnDownload_Click(object sender, EventArgs e) { Stream outputStream = ExecuteMerge("all", DocumentFormat.Rtf); outputStream.Seek(0, SeekOrigin.Begin); StreamCopyHelper.Copy(outputStream, Response.OutputStream); Response.StatusCode = (int)HttpStatusCode.OK; Response.ContentType = "application/rtf"; Response.AddHeader("Content-Disposition", "attachment; filename=RichEditMailMerge.rtf"); Response.End(); } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.