Products Technologies Demo Docs Blog Support Company

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.

Merge Excel Documents into MailMerge Templates using IncludeText Fields

IncludeText fields can be used to merge external references into a mail merge template using the reporting engine TXTextControl.DocumentServer.MailMerge class. External documents of type DOC, DOCX, RTF and TX can be referenced and are automatically included during the merge process.

The TXTextControl.DocumentServer.MailMerge.IncludeTextMerging event can be used to inject other files or formatted text by returning a document in the event. The following code shows how to handle Excel file references:

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 TXTextControl.ServerTextControl class. The Bookmark property of the IncludeTextField is used to define the sheet name in the Excel document. After the document is loaded successfully into ServerTextControl, it is exported in the internal TX Text Control format and returned to the TXTextControl.DocumentServer.MailMerge.IncludeTextMergingEventArgs.IncludeTextDocument property.

The following screen video shows this in action:

Screen video merge Excel

Download this sample from our GitHub repository and try this on your own.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • TXTextControl.DocumentServer.MailMerge Class
  • TXTextControl.DocumentServer.MailMerge.IncludeTextMerging Event
  • TXTextControl.DocumentServer.MailMerge.IncludeTextMergingEventArgs.IncludeTextDocument Property
  • TXTextControl.ServerTextControl Class

GitHub

Download and Fork This Sample on GitHub

We proudly host our sample code on github.com/TextControl.

Please fork and contribute.

Download ZIP

Open on GitHub

Open in Visual Studio

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.

See Reporting products

Related Posts

ASP.NETReportingWindows Forms

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…


ASP.NETReportingWindows Forms

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…


ASP.NETReportingWindows Forms

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…

Share on this blog post on: