Version

Export a DataPresenter Control to Excel

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

Introduction

The DataPresenter controls can export data in Microsoft® Excel® format using the DataPresenterExcelExporter class. The DataPresenterExcelExporter class encapsulates all the work of creating a workbook, iterating DataPresenter rows/cells, and setting worksheet cell values.

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

Note
Note

Due to the richness in styling available to the DataPresenter control, the DataPresenterExcelExporter object does not export a DataPresenter control’s styles to Excel. You can, however, format the resulting worksheet by accessing the Infragistics' Excel Engine objects directly or by applying formats before exporting.

Requirements

To be able to export data, you need the following references added to your project (in the Solution Explorer):

  • InfragisticsWPF.dll

  • InfragisticsWPF.Editors.dll

  • InfragisticsWPF.DataPresenter.dll

  • InfragisticsWPF.Documents.Excel.dll

  • InfragisticsWPF.DataPresenter.ExcelExporter.dll

Overview

Conceptual overview of the export procedure:

  1. Adding the grid

  1. Adding the Export button

  1. Adding the xamDataPresenter control

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

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

  1. Instantiating a DataPresenterExcelExporter object

  1. Calling the export method

  1. (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 and configure it as follows:

    • Set the button’s Content property to Export DataPresenter.

    • Attach an event handler to the button’s 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 and configure its properties as follows:

    • the Name property – to xamDataPresenter1.

    • the 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 that you will not have to type the fully qualified name of a member. If you want to add an instance of a DataPresenterExcelExporter 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:igExcelExporter="http://infragistics.com/ExcelExporter"

    In Visual Basic:

    Imports Infragistics.Windows.DataPresenter.ExcelExporter
    Imports Infragistics.Documents.Excel

    In C#:

    using Infragistics.Windows.DataPresenter.ExcelExporter;
    using Infragistics.Documents.Excel;
  1. (Conditional) Add an event handler for the Button control’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 DataPresenterExcelExporter object
        'TODO: Call the DataPresenterExcelExporter object's Export method
    End Sub

    In C#:

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

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

    In XAML:

    <Window.Resources>
        <igExcelExporter:DataPresenterExcelExporter x:Key="excelExporter1" />
    </Window.Resources>

    In Visual Basic:

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

    In C#:

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

    You can choose between two methods – Export and ExportAsync – depending on whether want to export data synchronously or asynchronously. The file extension of the exported file should match the target Excel version (.xlsx for Excel 2007/2010 or .xls for Excel 97/2003).

    • to export the data synchronously:

      Call the DataPresenterExcelExporter object’s Export method:

      In Visual Basic:

      exporter.Export(Me.xamDataPresenter1, "xamDataPresenter1.xlsx", WorkbookFormat.Excel2007)

      In C#:

      exporter.Export(this.xamDataPresenter1, "xamDataPresenter1.xlsx", WorkbookFormat.Excel2007);
    • to export the data asynchronously:

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

      In Visual Basic:

      exporter.ExportAsync(Me.xamDataPresenter1, "xamDataPresenter1.xlsx", WorkbookFormat.Excel2007)

      In C#:

      exporter.ExportAsync(this.xamDataPresenter1, "xamDataPresenter1.xlsx", WorkbookFormat.Excel2007);
    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. (Conditional) Verify your implementation.

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