New to Telerik UI for WPF? Download free 30-day trial

SpreadsheetStreamingExport

The GridViewSpreadStreamExport uses the RadSpreadStreamProcessing library which allows you to create big documents (without loading the entire document in the memory) and export them to the XLSX and CSV formats.

Assembly References

The Export with SpreadsheetStreamingExport functionality uses additional libraries so you need to add references to the following assemblies:

  • Telerik.Windows.Controls.dll
  • Telerik.Windows.Controls.Input.dll
  • Telerik.Windows.Controls.GridView.dll
  • Telerik.Windows.Controls.GridView.SpreadsheetStreamingExport.dll
  • Telerik.Documents.SpreadsheetStreaming.dll
  • Telerik.Windows.Data.dll
  • Telerik.Windows.Zip.dll

Telerik.Windows.Controls.GridView.SpreadsheetStreamingExport.dll is a new binary introduced in R1 2019.

GridViewSpreadStreamExport

In order to export RadGridView, you need to define an instance of the GridViewSpreadStreamExport class. You must then pass the instance of the RadGridView control that you want to be exported as a parameter. You can also specify the name of the sheet that will be exported as well as the ExportFormat - XLSX or CSV.

Example 1: Exporting RadGridView with GridViewSpreadStreamExport

GridViewSpreadStreamExport spreadStreamExport = new GridViewSpreadStreamExport(this.GridView); 
spreadStreamExport.SheetName = "Sheet1"; 
spreadStreamExport.ExportFormat = SpreadStreamExportFormat.Xlsx; 

RunExport

Once you've correctly initialized the GridViewSpreadStreamExport, you can call the RunExport method to trigger the export mechanism. The method itself has 4 different overloads:

  • RunExport(string fileName, SpreadStreamExportRenderer exportRenderer)
  • RunExport(string fileName, SpreadStreamExportRenderer exportRenderer, GridViewSpreadStreamExportOptions options)
  • RunExport(Stream exportStream, SpreadStreamExportRenderer exportRenderer)
  • RunExport(Stream exportStream, SpreadStreamExportRenderer exportRenderer, GridViewSpreadStreamExportOptions options)

As you can observe, you must either specify a file name or provide a stream for the GridViewSpreadStreamExport to work with and must create an instance of the SpreadStreamExportRenderer class which exposes the methods needed to export using RadSpreadStreamProcessing. Optionally, you can also pass in GridViewSpreadStreamExportOptions to customize the export to your needs.

Example 1: Exporting RadGridView with GridViewSpreadStreamExport

GridViewSpreadStreamExport spreadStreamExport = new GridViewSpreadStreamExport(this.GridView); 
spreadStreamExport.RunExport(dialog.FileName.ToString(), new SpreadStreamExportRenderer()); 

When the exporting operation is completed, the ExportCompleted event of GridViewSpreadStreamExport is raised.

RunExportAsync

You can also export RadGridView asynchronously by utilizing the RunExportAsync method of the GridViewSpreadStreamExport class. The method has the same overloads as its synchroneous counterpart:

  • RunExportAsync(string fileName, SpreadStreamExportRenderer exportRenderer)
  • RunExportAsync(string fileName, SpreadStreamExportRenderer exportRenderer, GridViewSpreadStreamExportOptions options)
  • RunExportAsync(Stream exportStream, SpreadStreamExportRenderer exportRenderer)
  • RunExportAsync(Stream exportStream, SpreadStreamExportRenderer exportRenderer, GridViewSpreadStreamExportOptions options)

Example 2: Exporting RadGridView Asynchronously

GridViewSpreadStreamExport spreadStreamExport = new GridViewSpreadStreamExport(this.GridView); 
spreadStreamExport.RunExportAsync(dialog.FileName.ToString(), new SpreadStreamExportRenderer()); 

When the progress of the asynchronous export changes, the AsyncExportProgressChanged event of GridViewSpreadStreamExport is triggered. Finally, when the export operation is over, the AsyncExportCompleted event is raised.

When using the RunExportAsync method you can also set the ShowLoadingIndicatorWhileAsyncExport property to True to display a RadBusyIndicator to the user and indicate that a time-consuming operation is taking place without blocking the UI. You can control the content of the indicator via RadGridView's BusyContent and BusyContentTemplate properties.

If you wish to cancel the export operation, you can also invoke the CancelExportAsync method.

GridViewSpreadStreamExportOptions

Via the GridViewSpreadStreamExportOptions you can customize how the RadGridView control is exported. The class exposes the following properties:

  • ExportDefaultStyles: Specifies whether the GridViewDataControl will be exported with its default styles.
  • ShowColumnFooters: Specifies whether column footers should be included on export.
  • ShowGroupFooters: Specifies whether group footers should be included on export.
  • ShowColumnGroups: Specifies whether common column headers should be included on export.
  • ShowGroupHeaderRowAggregates: Specifies whether group header aggregates should be included on export.
  • HiddenColumnExportOption: Gets or sets a value indicating how hidden columns are exported. The HiddenColumnExportOptions enumeration provides three possible options: ExportAlways, DoNotExport and ExportAsHidden.
  • ColumnWidth: Gets or sets the width of the columns that are exported.
  • Items: The collection of items to be exported.
  • ShowGroupRows: Specifies whether the group rows should be exported (this property was introduced in R3 2019).
  • Culture: Sets a specific Culture (this property was introduced in R3 2019).
  • IgnoreCollapsedGroups: Specifies whether the collapsed groups should be exported (this property was introduced in R3 2019).
  • ExcludedColumns: Specifies which columns to be excluded while exporting the control (this property was introduced in the 2020.2.622 internal build).

Example 3: Export RadGridView with headers, footers and default styles

GridViewSpreadStreamExport spreadStreamExport = new GridViewSpreadStreamExport(this.GridView); 
                           spreadStreamExport.RunExport(dialog.FileName.ToString(),  
                                                        new SpreadStreamExportRenderer(),  
                                                        new GridViewSpreadStreamExportOptions()  
                                                        {  
                                                            ShowColumnHeaders = true,  
                                                            ShowColumnFooters = true,  
                                                            ExportDefaultStyles=true  
                                                        }); 

Figure 1: Exporting with ExportDefaultStyles set to True

Telerik WPF DataGrid export-default-styles 2

Events

Apart from the previously discussed ExportCompleted, AsyncExportProgressChanged and AsyncExportCompleted events, GridViewSpreadStreamExport also exposes the ElementExportingToDocument and ElementExportedToDocument events which allow you to style and format the exported elements.

ElementExportingToDocument

The ElementExportingToDocument when an element is about to be exported. Via its GridViewSpreadStreamElementExportingEventArgs you can access the following properties:

  • Element: The type of the currently exported element. This can be any of the following types: HeaderCell, FooterCell, GroupHeaderCell, GroupFooterCell, Cell or CommonColumnHeader.
  • Value: The value that is about to be exported.
  • Cancel: A boolean value that indicates whether the event should be canceled or not.
  • Column: The column of the exported cell.
  • Style: The style for the exported cell. It is of type SpreadStreamCellStyle and provides a number of options for styling the exported element.

ElementExportedToDocument

The ElementExportedEvent on the other hand is fired each time an element is exported. Its GridViewSpreadStreamElementExportedEventArgs have a single property - Element, which holds the type of the exported element (HeaderCell, FooterCell, GroupHeaderCell, GroupFooterCell, Cell or CommonColumnHeader).

See Also

In this article