<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<!--TODO: Add a Button control-->
<!--TODO: Add a xamDataPresenter control-->
</Grid>
This topic explains how to implement data export to Excel using the xamDataPresenter™ control.
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.
To be able to export data, you first need to reference the following NuGet packages in your application:
Infragistics.WPF.DataGrids
Infragistics.WPF.DataGrids.Excel
For more information on setting up the NuGet feed and adding NuGet packages, you can take a look at the following documentation: NuGet Feeds.
Conceptual overview of the export procedure:
Adding the grid
Adding the Export button
Adding the xamDataPresenter control
Adding using/Imports directives to the code-behind
(Conditional) Adding an event handler for the Button’s Click event
Instantiating a DataPresenterExcelExporter object
Calling the export method
(Optional) Verify your implementation
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>
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" />
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>
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;
(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
}
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"];
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);
(Conditional) Verify your implementation.
Run your project and then click the Export button. This will export the xamDataPresenter control to Excel.