New to Telerik UI for ASP.NET MVC? Start a free 30-day trial
Color Alternating Rows when Exporting Grid to Excel
Environment
Product | Telerik UI for ASP.NET MVC Grid |
Product version | 2025.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:
-
Handle the
ExcelExport
event of the Grid:Razor.Events(e => e.ExcelExport("excelExport"))
-
Within the
ExcelExport
event handler, loop through the rows in the sheet, select each alternating row, and set thebackground
option of each cell to the desired color:JSfunction 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.