Merge Excel Documents into MailMerge Templates using IncludeText Fields
This sample shows how to use IncludeText fields to merge Excel documents into templates using MailMerge.

IncludeText fields can be used to merge external references into a mail merge template using the reporting engine TXText
The TXText
private void MailMerge_IncludeTextMerging(object sender,
TXTextControl.DocumentServer.MailMerge.IncludeTextMergingEventArgs e)
{
// custom handing of XLSX files
switch (Path.GetExtension(e.IncludeTextField.Filename))
{
case ".xlsx": // Excel file detected
if (!File.Exists(e.IncludeTextField.Filename))
return;
// load document into temp. ServerTextControl
using (TXTextControl.ServerTextControl tx =
new TXTextControl.ServerTextControl())
{
tx.Create();
// Bookmark name is the sheet name of the Excel document
TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings()
{
DocumentPartName = e.IncludeTextField.Bookmark
};
try
{
// load the Excel document
tx.Load(
e.IncludeTextField.Filename,
TXTextControl.StreamType.SpreadsheetML,
ls);
}
catch {
return;
}
byte[] data;
// save the document using the TX format
tx.Save(out data,
TXTextControl.BinaryStreamType.InternalUnicodeFormat);
e.IncludeTextDocument = data; // pass back to the field
}
break;
}
}
In case the reference ends with .xlsx, the file is loaded into a temporary TXText
The following screen video shows this in action:
Download this sample from our GitHub repository and try this on your own.
Also See
This post references the following in the documentation:
- TXText
Control. Document Server. Mail Merge Class - TXText
Control. Document Server. Mail Merge. Include Text Merging Event - TXText
Control. Document Server. Mail Merge. Include Text Merging Event Args. Include Text Document Property - TXText
Control. Server Text Control Class
Download and Fork This Sample on GitHub
We proudly host our sample code on github.com/TextControl.
Please fork and contribute.
Requirements for this sample
- Visual Studio 2017 or better
- TX Text Control .NET for Windows Forms X17 (trial sufficient)
Reporting
The Text Control Reporting Framework combines powerful reporting features with an easy-to-use, MS Word compatible word processor. Users can create documents and templates using ordinary Microsoft Word skills. The Reporting Framework is included in all .NET based TX Text Control products including ASP.NET, Windows Forms and WPF.
Related Posts
MailMerge: Conditional Table Cell Colors using Filter Instructions
The MailMerge class provides an extensible framework to inject custom logic to the merge process. This sample shows how to implement conditional table cell colors.
MailMerge: Using Filters to Remove Unwanted Rows
Data in merge blocks can be filtered and sorted. This article explains how to use filters to remove unwanted lines.
ReportingWindows FormsMail Merge
MailMerge: Conditional Rendering of Merge Blocks
Merge blocks can be used to render content conditionally. This article explains different ways to control this.
ReportingWindows FormsMail Merge
DataSourceManager: Using the Ready-to-Use Reporting Dialog Boxes
The Text Control DocumentServer class comes with ready-to-use dialog boxes for various purposes. This article shows how to use them in your application.
Different Ways to Create Documents using Text Control Products
There are many ways to create documents using Text Control products. This article gives an overview of different approaches using our products.