Products Technologies Demo Docs Blog Support Company

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.

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 MailMerge class provides an extensible framework to inject custom logic to the merge process. This sample shows how to implement conditional table cell colors.


ASP.NETReportingWindows Forms

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.


ASP.NETReportingWindows Forms

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.