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

Color Alternating Rows when Exporting Grid to Excel

Environment

ProductTelerik UI for ASP.NET Core 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.You can use this as a starting point to configure the same behavior in an ASP.NET Core project.

More ASP.NET Core Grid Resources

See Also