Spread Export
TreeViewSpreadExport utilizes our RadSpreadProcessing libraries to export the contents of RadTreeView to xlsx, csv, pdf or txt formats.
As of R3 2020 SP1 TreeViewSpreadExport also supports exporting to xls.
This article will explain in detail the SpreadExport capabilities and will demonstrate how you can use it.
Here is how the following RadTreeView, looks when it is exported.
Figure 1: Before and after export
The spread export functionality is located in the TelerikExport.dll assembly. You need to include the following namespace in order to access the types contained in TelerikExport:
- Telerik.WinControls.TelerikExport
Since this functionality is using the RadSpreadProcessingLibrary you need to reference the following assemblies as well:
- Telerik.Windows.Documents.Core
- Telerik.Windows.Documents.Fixed
- Telerik.Windows.Documents.Spreadsheet
- Telerik.Windows.Documents.Spreadsheet.FormatProviders.OpenXml
- Telerik.Windows.Documents.Spreadsheet.FormatProviders.Pdf
- Telerik.Windows.Zip
Exporting Data
To use the spread export functionality, an instance of the TreeViewSpreadExport object should be created, passing as parameter the RadtreeView instance to export. Afterwards, the RunExport method will trigger the export process. The latter method accepts as parameter a filename of the file to be exported.
You should pass an instance of a SpreadExportRenderer to the export method as well.
The RunExport method has several overloads allowing the user to export using a stream as well.
Running export synchronously using a stream
Properties
ExportFormat: Defines the format the TreeView will be exported to. The available values are Xslx, Pdf, Csv, Txt. The default value of the property is Xslx, hence if not other specified, the exporter will export to Xslx.
ExportVisualSettings: Allows you to export the visual settings (themes) to the exported file. RadTreeView will also export all formatting to the Excel file.
-
SheetMaxRows: The exporter splits the data on separate sheets if the number of rows is greater than the Excel maximum. You can control the maximum number of rows through this SheetMaxRows property. Available options are:
1048576: Max rows for Excel 2007 and above
65536 (default): Max rows for previous versions of Excel. This is the default setting.
SheetName: Defines the sheet name of the sheet to export to. If your data is large enough to be split on more than one sheets, then the export method adds index to the names of the next sheets.
-
FileExportMode: This property determines whether the data will be exported into an existing or a new file. If new is chosen and such exists it will be overridden. Available options are:
NewSheetInExistingFile: This option will create a new sheet in an already existing file.
CreateOrOverrideFile: Creates new or overrides an existing file.
ExportImages: Gets or sets a value indicating whether to export images.
ExportChildNodesGrouped: Gets or sets a value indicating whether to export child nodes grouped.
NodeIndent: Gets or sets the indent of child nodes.
CollapsedNodeOption: Gets or sets a value indicating how children of collapsed nodes are exported.
ExportCheckBoxes: Gets or sets a value indicating whether to export the check box values, when they are shown in tree nodes.
ExportCheckBoxes property is available since R1 2021.
Events
-
CellFormatting: This event is used to format the cells to be exported. The event arguments provide:
TreeNode: Gives you access to the currently exported node.
ExportCell: Allows you to set the styles of the exported cell.
RowIndex: The index of the currently exported row. Here is an example of formatting the exported TreeView:
Figure 2: Export using formating
- ExportCompleted: This event is triggered when the export operation completes.
Async Spread Export
RadTreeView provides functionality for asynchronous spread export. This feature can be utilized by calling the RunExportAsync method on the TreeViewSpreadExport object.
If the ExportVisualSettings property is set to true the UI can be freezed at some point. This is expected since exporting the visual settings requires creating visual elements for all nodes and updating the exported control UI.
The following example will demonstrate how the async spread export feature can be combined with a RadProgressBar control to deliver better user experience.
Fig.3 Exporting Data Asynchronously
- The following code shows how you can subscribe to the notification events and start the async export operation.
2. Handle the notification events and report progress.
The RunExportAsync method has several overloads allowing the user to export using a stream as well:
Running export asynchronously using a stream
Async Export Methods and Events
Methods
The following methods of the TreeViewSpreadExport class are responsible for exporting the data.
RunExportAsync: Starts an export operation which runs in a background thread.
CancelExportAsync: Cancels an export operation.
Events
The following events provide information about the state of the export operation.
AsyncExportProgressChanged: Occurs when the progress of an asynchronous export operation changes.
AsyncExportCompleted: Occurs when an async export operation is completed.