Version

Exporting Data to Word Using the xamDataPresenter

This topic explains how to implement data export to Word using the xamDataPresenter™ control.

Introduction

The DataPresenter controls can export data in Microsoft® Word® format using the DataPresenterWordWriter class. The DataPresenterWordWriter class encapsulates all the work of creating a Word document containing tables representing the values/layout of a specified DataPresenterBase.

The DataPresenterWordWriter class is not a visual element; therefore, you cannot add it to your window’s visual tree. However, if you want to instantiate a DataPresenterWordWriter object in XAML, you can add it to your window’s resource dictionary.

Note
Note

Due to the flexibility in styling the DataPresenter control, the DataPresenterWordWriter object does not export a DataPresenter control’s styles to Word. However, you can format the resulting document by accessing the Infragistics' Word Engine objects directly or by applying formats before exporting.

Requirements

To add be able to export data, you need to reference the following NuGet packages:

  • Infragistics.WPF.DataGrids

  • Infragistics.WPF.DataGrids.Word

For more information on setting up the NuGet feed and adding NuGet packages, you can take a look at the following documentation: NuGet Feeds.

Overview

Conceptual overview of the export procedure:

  1. Adding the grid

  2. Adding the Export button

  3. Adding the xamDataPresenter control

  4. Adding using/Imports directives to the code-behind

  5. (Conditional) Adding an event handler for the Button’s Click event

  6. Instantiating a DataPresenterWordWriter object

  7. Calling the export method

  8. (Optional) Verify your implementation

Steps

  1. Add the grid.

    Add a Grid panel with two RowDefinition objects to your window.

    In XAML:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <!--TODO: Add a Button control-->
        <!--TODO: Add a xamDataPresenter control-->
    </Grid>
  1. Add the Export button.

    Add a Button control to the first row in the Grid panel.

    • Set its Content property to "Export DataPresenter".

    • Attach an event handler to its Click event.

    In XAML:

    <Button Content="Export DataPresenter" Click="Button_Click" />
  1. Add the xamDataPresenter control.

    Add a xamDataPresenter™ control to the second row in the Grid panel.

    • Set its Name property to xamDataPresenter1.

    • Set its BindToSampleData property to True.

    In XAML:

    <igDP:XamDataPresenter Name="xamDataPresenter1" BindToSampleData="True">
    </igDP:XamDataPresenter>
  1. Add using/Imports directives to the code-behind.

    Open the code-behind and place using/Imports directives in your code-behind so you don’t have to type out a member’s fully qualified name. If you want to add an instance of a DataPresenterWordWriter object to your window’s resource dictionary instead of instantiating it in code, you will need to add an XML namespace declaration.

    In XAML:

    xmlns:igWordWriter="http://infragistics.com/WordWriter"

    In Visual Basic:

    imports Infragistics.Windows.DataPresenter.WordWriter

    In C#:

    using Infragistics.Windows.DataPresenter.WordWriter;
  1. (Conditional) Add an event handler for the Button’s Click event.

    If a method stub has not been created for you, you will need to add an event handler for the Button control’s Click event.

    In Visual Basic:

    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        'TODO: Instantiate a DataPresenterWordWriter object
        'TODO: Call the DataPresenterWordWriter object's Export method
    End Sub

    In C#:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        //TODO: Instantiate a DataPresenterWordWriter object
        //TODO: Call the DataPresenterWordWriter object's Export method
    }
  1. Instantiate a DataPresenterWordWriter object.

    If you are instantiating it in XAML, you can add it to your window’s resource dictionary.

    In XAML:

    <Window.Resources>
        <igWordWriter:DataPresenterWordWriter x:Key="wordWriter1" />
    </Window.Resources>

    In Visual Basic:

    Dim writer As New DataPresenterWordWriter()
    ' If you added the DataPresenterWordWriter object to your
    ' window's resource dictionary, use these lines of code:
    ' Dim writer As DataPresenterWordWriter = _
    '     DirectCast(Me.Resources("wordWriter1"), DataPresenterWordWriter)

    In C#:

    DataPresenterWordWriter writer = new DataPresenterWordWriter();
    // If you added the DataPresenterWordWriter object to your
    // window's resource dictionary, use these lines of code:
    // DataPresenterWordWriter writer =
    //     (DataPresenterWordWriter)this.Resources["wordWriter1"];
  1. Call the export method.

    You can choose between two methods – Export and ExportAsync – depending on whether want to export data synchronously or asynchronously.

    • to export the data synchronously:

      Call the DataPresenterWordWriter object’s Export method:

      In Visual Basic:

      writer.Export(Me.xamDataPresenter1, "xamDataPresenter1.docx")

      In C#:

      writer.Export(this.xamDataPresenter1, "xamDataPresenter1.docx");
    • to export the data asynchronously:

      Call the DataPresenterWordWriter object’s ExportAsync to export the data asynchronously:

      In Visual Basic:

      writer.ExportAsync(Me.xamDataPresenter1, "xamDataPresenter1.docx")

      In C#:

      writer.ExportAsync(this.xamDataPresenter1, "xamDataPresenter1.docx");
    Note
    Notes on asynchronous export:
    1. The asynchronous export is exporting the data in chunks. Each chunk is processed for the amount of time specified by the AsyncExportDuration property and the time between two chunk processing is specified by the AsyncExportInterval property.

    2. If you invoke the ExportAsync method and want to track when the export process has finished it is a good idea to hook at the ExportEnded event.

  1. (Optional) Verify your implementation.

    Run your project and then click the Export button. This will export the xamDataPresenter control to Word.