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

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

WinForms RadTreeView Before export

WinForms RadTreeView 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.

TreeViewSpreadExport exporter = new TreeViewSpreadExport(this.radTreeView1);
SpreadExportRenderer renderer = new SpreadExportRenderer();
exporter.RunExport(@"C:\ExportedFile.xlsx", renderer);

Dim exporter As New TreeViewSpreadExport(Me.radTreeView1)
Dim renderer As New SpreadExportRenderer()
exporter.RunExport("C:\ExportedFile.xlsx", renderer)

The RunExport method has several overloads allowing the user to export using a stream as well.

Running export synchronously using a stream

string exportFile = @"..\..\exportedData.xlsx";
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
    Telerik.WinControls.Export.TreeViewSpreadExport spreadExporter = new Telerik.WinControls.Export.TreeViewSpreadExport(this.radTreeView1);
    Telerik.WinControls.Export.SpreadExportRenderer spreadRenderer = new Telerik.WinControls.Export.SpreadExportRenderer();
    spreadExporter.RunExport(ms, spreadRenderer);
    using (System.IO.FileStream fileStream = new System.IO.FileStream(exportFile, FileMode.Create, FileAccess.Write))
    {
        ms.WriteTo(fileStream);
    }
}

Dim exportFile As String = "..\..\exportedData.xlsx"
Using ms As New System.IO.MemoryStream()
    Dim spreadExporter As New Telerik.WinControls.Export.TreeViewSpreadExport(Me.radTreeView1)
    Dim spreadRenderer As New Telerik.WinControls.Export.SpreadExportRenderer()
    spreadExporter.RunExport(ms, spreadRenderer)
    Using fileStream As New System.IO.FileStream(exportFile, FileMode.Create, FileAccess.Write)
        ms.WriteTo(fileStream)
    End Using
End Using

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:

void exporter_CellFormatting(object sender, TreeViewSpreadExportCellFormattingEventArgs e)
{
    e.ExportCell.BackColor = ColorTranslator.FromHtml("#F4FFEC");
    e.ExportCell.Font = new Font("Consolas", 10, FontStyle.Underline);
}

Private Sub exporter_CellFormatting(ByVal sender As Object, ByVal e As TreeViewSpreadExportCellFormattingEventArgs)
    e.ExportCell.BackColor = ColorTranslator.FromHtml("#F4FFEC")
    e.ExportCell.Font = New Font("Consolas", 10, FontStyle.Underline)
End Sub

Figure 2: Export using formating

WinForms RadTreeView 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

WinForms RadTreeView Exporting Data Asynchronously

  1. The following code shows how you can subscribe to the notification events and start the async export operation.
private void btnExportAsync_Click(object sender, EventArgs e)
{
    TreeViewSpreadExport spreadExporter = new TreeViewSpreadExport(this.radTreeView1);
    spreadExporter.AsyncExportProgressChanged += spreadExporter_AsyncExportProgressChanged;
    spreadExporter.AsyncExportCompleted += spreadExporter_AsyncExportCompleted;
    SpreadExportRenderer exportRenderer = new SpreadExportRenderer();
    spreadExporter.RunExportAsync(@"..\..\ExportedFile.xlsx", exportRenderer);
}

Private Sub btnExportAsync_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim spreadExporter As New TreeViewSpreadExport(Me.radTreeView1)
    AddHandler spreadExporter.AsyncExportProgressChanged, AddressOf spreadExporter_AsyncExportProgressChanged
    AddHandler spreadExporter.AsyncExportCompleted, AddressOf spreadExporter_AsyncExportCompleted
    Dim exportRenderer As New SpreadExportRenderer()
    spreadExporter.RunExportAsync("..\..\ExportedFile.xlsx", exportRenderer)
End Sub

2. Handle the notification events and report progress.

private void spreadExporter_AsyncExportProgressChanged(object sender, ProgressChangedEventArgs e)
{
    this.radProgressBar1.Value1 = e.ProgressPercentage;
}
private void spreadExporter_AsyncExportCompleted(object sender, AsyncCompletedEventArgs e)
{
    RadMessageBox.Show("Async Spread Export Completed!");
    this.radProgressBar1.Value1 = 0;
}

Private Sub spreadExporter_AsyncExportProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
    Me.radProgressBar1.Value1 = e.ProgressPercentage
End Sub
Private Sub spreadExporter_AsyncExportCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
    RadMessageBox.Show("Async Spread Export Completed!")
    Me.radProgressBar1.Value1 = 0
End Sub

The RunExportAsync method has several overloads allowing the user to export using a stream as well:

Running export asynchronously using a stream


private void buttonRunExportAsync_Click(object sender, EventArgs e)
{
    System.IO.MemoryStream ms = new System.IO.MemoryStream();         
    Telerik.WinControls.Export.TreeViewSpreadExport  spreadExporter = new Telerik.WinControls.Export.TreeViewSpreadExport (this.radTreeView1);
    Telerik.WinControls.Export.SpreadExportRenderer spreadRenderer = new Telerik.WinControls.Export.SpreadExportRenderer();
    spreadExporter.AsyncExportCompleted += exporter_AsyncExportCompleted;
    spreadExporter.RunExportAsync(ms, spreadRenderer);
}

private void exporter_AsyncExportCompleted(object sender, AsyncCompletedEventArgs e)
{
    RunWorkerCompletedEventArgs args = e as RunWorkerCompletedEventArgs;
    string exportFile = @"..\..\exportedAsyncData.xlsx";
    using (System.IO.FileStream fileStream = new System.IO.FileStream(exportFile, FileMode.Create, FileAccess.Write))
    { 
        MemoryStream ms = args.Result as MemoryStream;
        ms.WriteTo(fileStream);
        ms.Close();
    }
}

Private Sub buttonRunExportAsync_Click(sender As Object, e As EventArgs)
    Dim ms As New System.IO.MemoryStream()
    Dim spreadExporter As New Telerik.WinControls.Export.TreeViewSpreadExport(Me.radTreeView1)
    Dim spreadRenderer As New Telerik.WinControls.Export.SpreadExportRenderer()
    AddHandler spreadExporter.AsyncExportCompleted, AddressOf exporter_AsyncExportCompleted
    spreadExporter.RunExportAsync(ms, spreadRenderer)
End Sub
Private Sub exporter_AsyncExportCompleted(sender As Object, e As AsyncCompletedEventArgs)
    Dim args As RunWorkerCompletedEventArgs = TryCast(e, RunWorkerCompletedEventArgs)
    Dim exportFile As String = "..\..\exportedAsyncData.xlsx"
    Using fileStream As New System.IO.FileStream(exportFile, FileMode.Create, FileAccess.Write)
        Dim ms As MemoryStream = TryCast(args.Result, MemoryStream)
        ms.WriteTo(fileStream)
        ms.Close()
    End Using
End Sub

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.

In this article