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

Color Alternating Rows when Exporting Grid to Excel

Environment

ProductTelerik UI for ASP.NET MVC Grid
Product version2025.1.227

Description

How can I color the alternating rows in the Excel file that contains the Grid data?

Solution

The example relies on the following key steps:

  1. Handle the ExcelExport event of the Grid:

    Razor
    .Events(e => e.ExcelExport("excelExport"))
  2. Within the ExcelExport event handler, loop through the rows in the sheet, select each alternating row, and set the background option of each cell to the desired color:

    JS
        function excelExport(e) {
            var sheet = e.workbook.sheets[0];
            for (var rowIndex = 1; rowIndex < sheet.rows.length; rowIndex++) {
                if (rowIndex % 2 == 0) {
                    var row = sheet.rows[rowIndex];
                    for (var cellIndex = 0; cellIndex < row.cells.length; cellIndex++) {
                        row.cells[cellIndex].background = "#aabbcc";
                    }
                }
            }
        }

To review the complete example, refer to the ASP.NET MVC project on coloring alternating rows 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