Merge Excel Documents into MailMerge Templates using IncludeText Fields
IncludeText fields in TX Text Control templates reference external Excel files during mail merge. The process loads .xlsx files as temporary ServerTextControl instances, using Bookmark to target specific sheets. Merged Excel content renders directly within the document template output.

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 TX Text Control MailMerge FieldMerged event exposes TableCell instances during merge for conditional formatting. A CellFilterInstructions class parses filter rules from merge field names and…
MailMerge: Using Filters to Remove Unwanted Rows
TX Text Control merge block filters remove unwanted rows from repeating data during the mail merge process without modifying the underlying source. Filter conditions applied to merge blocks…
ReportingWindows FormsMail Merge
MailMerge: Conditional Rendering of Merge Blocks
TX Text Control X16 provides two approaches to conditionally render merge blocks within MailMerge templates. Setting RemoveEmptyBlocks removes blocks when child data is absent, while…
ReportingWindows FormsMail Merge
DataSourceManager: Using the Ready-to-Use Reporting Dialog Boxes
The TX Text Control DocumentServer namespace ships with seven ready-to-use reporting dialogs for data selection tasks: chart data relations, filter and sort, database connection, data source…
Different Ways to Create Documents using Text Control Products
Text Control offers four document creation paths: building from scratch via the ServerTextControl API, merging pre-designed templates with MailMerge using IEnumerable or DataSet sources, editing…
