Xceed DataGrid for WPF v7.3 Documentation

In This Topic
    Exporting to Excel (ExcelExporter Class)
    In This Topic

    The following example demonstrates how to export the content of a grid to Excel using the ExcelExporter class.

    XAML
    Copy Code
    <Grid xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid">
      <Grid.Resources>
         <xcdg:DataGridCollectionViewSource x:Key="cvs_orders"
                                            Source="{Binding Source={x:Static Application.Current}, Path=Orders}">
            <xcdg:DataGridCollectionViewSource.GroupDescriptions>
               <xcdg:DataGridGroupDescription PropertyName="ShipCountry" />
            </xcdg:DataGridCollectionViewSource.GroupDescriptions>
           
            <xcdg:DataGridCollectionViewSource.StatFunctions>
               <xcdg:AverageFunction SourcePropertyName="Freight"
                                     ResultPropertyName="average_freight" />
            </xcdg:DataGridCollectionViewSource.StatFunctions>
         </xcdg:DataGridCollectionViewSource>
      </Grid.Resources>
      <DockPanel>
         <Button Content="Export"
                 Click="ExportButton_Click"
                 DockPanel.Dock="Top" />
         <xcdg:DataGridControl x:Name="OrdersGrid"
                               ItemsSource="{Binding Source={StaticResource cvs_orders}}"
                               AutoCreateDetailConfigurations="True">
            <xcdg:DataGridControl.DefaultGroupConfiguration>
               <xcdg:GroupConfiguration>
                  <xcdg:GroupConfiguration.Footers>
                     <DataTemplate>
                        <xcdg:StatRow>
                           <xcdg:StatCell FieldName="Freight"
                                          ResultPropertyName="average_freight"/>
                        </xcdg:StatRow>
                     </DataTemplate>
                  </xcdg:GroupConfiguration.Footers>
               </xcdg:GroupConfiguration>
            </xcdg:DataGridControl.DefaultGroupConfiguration>
         </xcdg:DataGridControl>
      </DockPanel>
    </Grid>

    The following code provides the code-behind implementation of the button's Click event in which the ExcelExporter object is configured and used to export the content.

    VB.NET
    Copy Code
    Private Sub ExportButton_Click( ByVal sender As Object, ByVal e As RoutedEventArgs )
      Dim exporter As New ExcelExporter( Me.OrdersGrid )
      ' All details
      exporter.DetailDepth = Int.MaxValue
      ' The grid (0) and groups (1)
      exporter.StatFunctionDepth = 1
      exporter.ShowStatsInDetails = False
      exporter.ExportStatFunctionsAsFormulas = False
      exporter.Export( "d:\orders.xls" )
    End Sub
    C#
    Copy Code
    private void ExportButton_Click( object sender, RoutedEventArgs e )
    {
     ExcelExporter exporter = new ExcelExporter( this.OrdersGrid );
     // All details
     exporter.DetailDepth = int.MaxValue;
     // The grid (0) and groups (1)
     exporter.StatFunctionDepth = 1;     
     exporter.ShowStatsInDetails = false;
     exporter.ExportStatFunctionsAsFormulas = false;
     exporter.Export( "d:\\orders.xls" );
    }