New to Telerik UI for ASP.NET MVCStart a free 30-day trial

Use Column Templates when Exporting Grid to Excel

Environment

ProductTelerik UI for ASP.NET MVC Grid
Product version2025.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:

  1. Add the column in the Grid that has a template:

    Razor
    columns.Bound(p => p.Freight).ClientTemplate("Freight: #: kendo.format('{0:c}', Freight) #");
  2. Handle the ExcelExport event of the Grid:

    Razor
    .Events(e => e.ExcelExport("excelExport"))
  3. 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 the kendo.template() method to specify the column template as a value of the cell.

    JS
    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]);
        }
    }

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.

More ASP.NET MVC Grid Resources

See Also