# Exporting Grid Column Templates to Excel

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Progress Telerik UI for ASP.NET MVC Grid</td>
 </tr>
 <tr>
  <td>Progress Telerik UI for ASP.NET MVC version</td>
  <td>Created with the 2022.3.913 version</td>
 </tr>
</table>

## Description

I use the Grid's Excel Export configuration but I also need to export the content of the Column Templates. How can I achieve this in the Telerik UI for ASP.NET MVC Grid?

## Solution

To export the content of the Column Templates:

1. Subscribe to the [`ExcelExport` Event](https://www.telerik.com/aspnet-mvc/documentation/api/kendo.mvc.ui.fluent/grideventbuilder#excelexportsystemstring)  of the Grid.

2. In the handler get the first sheet of the [workbook](https://docs.telerik.com/kendo-ui/api/ooxml/workbook#configuration). This allows you to modify its rows later.

3. Access the Grid's client-side instance that is available in the event's content. Then get the column by its index.

4. Evaluate the template.

5. Get the `dataItems` set that is currently loaded in the Grid.

6. Iterate the `dataItems`. Use the evaluated template as a value for the respective columns.

```C#
@{
    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
	.Name("grid")
	
	.Columns(columns =>
	{
		columns.Bound(p => p.ProductName).Width(300);
		columns.Bound(p => p.UnitPrice).ClientTemplate("Price per unit: #: kendo.format('{0:c}', UnitPrice) #").Width(300);
		columns.Bound(p => p.UnitsOnOrder).Width(300);
		columns.Bound(p => p.UnitsInStock).Width(300);
	})
	.ToolBar(tools => tools.Excel())
	.Pageable()
	.Sortable()
	.Scrollable()
	.Groupable()
	.Excel(excel => excel
		.FileName("Kendo UI Grid Export.xlsx")
		.Filterable(true)
		.ProxyURL(Url.Action("Excel_Export_Save", "Grid"))
	)
	.Events(e => e.ExcelExport("excelExport"))
	.Reorderable(r => r.Columns(true))
	.Resizable(r => r.Columns(true))
	.ColumnMenu()
	.DataSource(dataSource => dataSource
		.Ajax()
		.PageSize(10)
		.Read(read => read.Action("Excel_Export_Read", "Grid"))
	)
)
```

```JavaScript
    function excelExport(e) {
        var sheet = e.workbook.sheets[0];
        var template = kendo.template(this.columns[1].template);
        var data = this.dataSource.view();
        for (var i = 0; i < data.length; i++) {
            sheet.rows[i + 1].cells[1].value = template(data[i]);
        }
    } 
```

For the complete implementation of the suggested approach, refer to this [Telerik REPL Example](https://netcorerepl.telerik.com/GGajGWPJ51YAIs9257).

To learn more about the Kendo UI templating engine, see the [Kendo UI Templates Article](https://docs.telerik.com/kendo-ui/framework/templates/overview).

## 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 Grid Column Templates to Excel](https://netcorerepl.telerik.com/GGajGWPJ51YAIs9257)
* [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)
