# Exporting Multiple Grids to Excel

## Environment

<table>
	<tbody>
		<tr>
			<td>Product Version</td>
			<td>2022.2.621</td>
		</tr>
		<tr>
			<td>Product</td>
			<td>Telerik UI for ASP.NET MVC Grid</td>
		</tr>
	</tbody>
</table>

## Description

How can I export multiple Grids to the same Excel file?

## Solution

The example below relies on the following key steps:

1. Create an external button to export the data when it is clicked.
2. Use the client-side [`saveAsExcel` method](https://docs.telerik.com/kendo-ui/api/ui/grid/methods/saveasexcel) to trigger the data export of each Grid.
3. Handle the [`ExcelExport`](https://www.telerik.com/aspnet-mvc/documentation/api/kendo.mvc.ui.fluent/grideventbuilder#excelexportsystemstring) event of the two Grids and prevent their default action.
4. Create a new Workbook by using the sheets of the Grids Workbooks and save it through the [`kendo.saveAs()` method](https://docs.telerik.com/kendo-ui/api/kendo/methods/saveas).

```Razor Index.cshtml
    //Export to Excel Button
    @(Html.Kendo().Button()
        .Name("exportData")
        .Content("Export to Excel")
        .Icon("file-excel")
        .Events(ev => ev.Click("exportDataClick"))
    )

    //First Grid
    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
        .Name("products")
        .Columns(columns =>
        {
            columns.Bound(p => p.ProductName);
            columns.Bound(p => p.UnitPrice);
            columns.Bound(p => p.UnitsOnOrder);
            columns.Bound(p => p.UnitsInStock);
        })
        .Events(e => e.ExcelExport("products_excelExport"))
        .Pageable()
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(20)
            .Read(read => read.Action("Excel_Export_Read", "Grid"))
        )
    )
    //Second Grid
    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.OrderViewModel>()    
        .Name("orders")
        .Columns(columns => {
            columns.Bound(p => p.OrderID).Filterable(false).Width(100);
            columns.Bound(p => p.Freight).Width(100);
            columns.Bound(p => p.OrderDate).Format("{0:MM/dd/yyyy}").Width(140);
            columns.Bound(p => p.ShipName);
            columns.Bound(p => p.ShipCity).Width(150);
        })
        .Events(e => e.ExcelExport("orders_excelExport"))
        .Pageable()
        .DataSource(dataSource => dataSource
            .Ajax()
            .PageSize(20)
            .Read(read => read.Action("Orders_Read", "Grid"))
        )
    )
```

```JavaScript
    
    
    <script src="https://unpkg.com/jszip/dist/jszip.min.js"></script>
    <script>
        // Use Promises to sync the exports.
        var promises = [
            $.Deferred(),
            $.Deferred()
        ];

        function exportDataClick(e) { //"Export to Excel" button "click" event handler.
            // Trigger export of the "products" Grid.
            $("#products").data("kendoGrid").saveAsExcel();
            // Trigger export of the "orders" Grid.
            $("#orders").data("kendoGrid").saveAsExcel();
            // Wait for both exports to finish.
            $.when.apply(null, promises)
            .then(function (productsWorkbook, ordersWorkbook) {
                // Create a new workbook using the sheets of the "products" and "orders" workbooks.
                var sheets = [
                    productsWorkbook.sheets[0],
                    ordersWorkbook.sheets[0]
                ];
                sheets[0].title = "Products";
                sheets[1].title = "Orders";
                var workbook = new kendo.ooxml.Workbook({
                    sheets: sheets
                });
                // Save the new workbook.
                workbook.toDataURLAsync().then(function(dataURL) {
                    kendo.saveAs({
                        dataURI: dataURL,
                        fileName: "ProductsAndOrders.xlsx"
                    });
                });

                promises = [
                    $.Deferred(),
                    $.Deferred()
                ];
            });
        }
        function products_excelExport(e) { //"ExcelExport" event handler of "products" Grid.
            e.preventDefault();
            promises[0].resolve(e.workbook);
        }
        function orders_excelExport(e) { //"ExcelExport" event handler of "orders" Grid.
            e.preventDefault();
            promises[1].resolve(e.workbook);
        }
    </script>
```

> Starting with Kendo UI version 2024.1.130 (Q1 2024), the JSZip library is no longer distributed with Kendo UI. You must include it from an official distribution channel such as `unpkg` or Cloudflare. Ensure the JSZip script loads successfully before attempting Excel export operations.

> The solution requires Telerik UI for ASP.NET MVC 2014.3.1125 version or later. Earlier versions do not expose the `ExcelExport` event through the fluent API.

To review the complete example, refer to the [ASP.NET MVC project on how to export multiple Grids to the same Excel file](https://github.com/telerik/ui-for-aspnet-mvc-examples/tree/master/Telerik.Examples.Mvc/Telerik.Examples.Mvc/Areas/GridExportingExcelMultiple).

## More ASP.NET MVC Grid Resources

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

* [ASP.NET MVC Grid Demos](https://demos.telerik.com/aspnet-mvc/grid/index)

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

* [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

* [Telerik REPL: Exporting Multiple Grids to Excel](https://netcorerepl.telerik.com/wcYKwCPR52YDsbfP56)
* [Exporting to Excel (Overview)](https://docs.telerik.com/aspnet-mvc/html-helpers/data-management/grid/export/excel-export)
* [Client-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/grid)
* [Server-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/grid)
* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2023-backwards-compatibility)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
