# Export Multiple TreeLists to Excel

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>TreeList for Telerik UI for ASP.NET MVC</td>
 </tr>
 <tr>
  <td>Product Version</td>
  <td>Created with version 2024.4.1112</td>
 </tr>
</table>

## Description

How can I export multiple ASP.NET MVC TreeList components to a single Excel document?

## Solution

In this example, each TreeList is exported to a separate Excel sheet. For more information on the Excel generation, refer to the [Excel export overview article](https://docs.telerik.com/kendo-ui/framework/excel/introduction).

1. Create an external button to export the TreeLists data when it is clicked.
2. Use the client-side [`saveAsExcel()`](https://docs.telerik.com/kendo-ui/api/ui/treelist/methods/saveasexcel) method to trigger the data export of each TreeList.
3. Handle the [`ExcelExport`](https://www.telerik.com/aspnet-mvc/documentation/api/kendo.mvc.ui.fluent/treelisteventbuilder#excelexportsystemstring) event of the TreeLists and prevent its default action.
4. Create a new Workbook that contains the sheets with the TreeLists data and save it through the [`kendo.saveAs()`](https://docs.telerik.com/kendo-ui/api/kendo/methods/saveas) method.

```Razor HtmlHelper
<script src="//cdnjs.cloudflare.com/ajax/libs/jszip/2.4.0/jszip.min.js"></script>

<button class="k-button" id="export">Export TreeLists to Excel</button>

<h3>Designers</h3>
@(Html.Kendo().TreeList<Telerik.Examples.Mvc.Areas.TreeListExportingMultiple.Models.EmployeeDirectoryModel>()
    .Name("designers")
    .Events(e => e.ExcelExport("onExcelExportDesigners"))
    ... // Additional configuration.
)
<h3>Lead personnel</h3>
@(Html.Kendo().TreeList<Telerik.Examples.Mvc.Areas.TreeListExportingMultiple.Models.EmployeeDirectoryModel>()
    .Name("leads")
    .Events(e => e.ExcelExport("onExcelExportLead"))
    ... // Additional configuration.
)
```

```JS Scripts
<script>
    function onExcelExportDesigners(e) {
        e.preventDefault();
        promises[0].resolve(e.workbook);
    }

    function onExcelExportLead(e) {
        e.preventDefault();
        promises[1].resolve(e.workbook);
    }

    // Used to sync the exports.
    var promises = [
        $.Deferred(),
        $.Deferred()
    ];

    $(document).ready(function(){
        $("#export").click(function (e) {
            // Trigger the export of the "designers" TreeList.
            $("#designers").data("kendoTreeList").saveAsExcel();
            // Trigger the export of the "leads" TreeList.
            $("#leads").data("kendoTreeList").saveAsExcel();
            // Wait for both exports to finish.
            $.when.apply(null, promises)
                .then(function (designersWorkbook, leadsWorkbook) {

                    // Create a new workbook using the sheets of the "designers" and "leads" workbooks.
                    var sheets = [
                        designersWorkbook.sheets[0],
                        leadsWorkbook.sheets[0]
                    ];

                    sheets[0].title = "Designers";
                    sheets[1].title = "Leads";

                    var workbook = new kendo.ooxml.Workbook({
                        sheets: sheets
                    });

                    promises = [
                        $.Deferred(),
                        $.Deferred()
                    ];

                    // Save the new workbook.
                    kendo.saveAs({
                        dataURI: workbook.toDataURL(),
                        fileName: "DesignersAndLeaders.xlsx"
                    });
                });
        });
    });
</script>
```

For a runnable example, refer to the [ASP.NET MVC application on how to export the data of multiple TreeLists in a single Excel document](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/TreeListExportingMultiple).

## More ASP.NET MVC TreeList Resources

* [ASP.NET MVC TreeList Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/treelist/overview)

* [ASP.NET MVC TreeList Demos](https://demos.telerik.com/aspnet-mvc/treelist)

* [ASP.NET MVC TreeList Product Page](https://www.telerik.com/aspnet-mvc/treelist)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Getting Started with Excel Export](https://docs.telerik.com/kendo-ui/framework/excel/get-started)

* [Server-Side API Reference of the TreeList for ASP.NET MVC](https://www.telerik.com/aspnet-mvc/documentation/api/treelist)

* [Client-Side API Reference of the TreeList for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/treelist)

* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2024-backwards-compatibility)

* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
