New to Telerik UI for ASP.NET MVC? Start a free 30-day trial
Use Column Templates when Exporting Grid to Excel
Environment
Product | Telerik UI for ASP.NET MVC Grid |
Product version | 2025.1.227 |
Description
How can I use column templates when exporting Grid's data to Excel?
Solution
The example relies on the following key steps:
-
Add the column in the Grid that has a template:
Razorcolumns.Bound(p => p.Freight).ClientTemplate("Freight: #: kendo.format('{0:c}', Freight) #");
-
Handle the
ExcelExport
event of the Grid:Razor.Events(e => e.ExcelExport("excelExport"))
-
Within the
ExcelExport
event handler, loop through the data items of the DataSource, select the row and cell in the sheet by index, and use thekendo.template()
method to specify the column template as a value of the cell.JSfunction 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]); } }
To review the complete example, refer to the ASP.NET MVC project on using column templates in the Grid Excel exports.
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.