<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 Word using the xamDataPresenter™ control.
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.
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.
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 DataPresenterWordWriter 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.
Set its Content property to "Export DataPresenter".
Attach an event handler to its 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.
Set its Name property to xamDataPresenter1.
Set its 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 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;
(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
}
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"];
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");
(Optional) Verify your implementation.
Run your project and then click the Export button. This will export the xamDataPresenter control to Word.