How to export ASP.NET MVC view data to an excel file ? Actually my view page contain many viewdata types. I am using for each loop with these datatype to displaying data on
Depending on the complexity of the page's HTML you might be able to get away with just changing the header type to application/excel
. Excel has built in html parsing functionality. I've successfully done this many times a few years back with a DataGrid on a page.
Response.ContentType = "application/excel"
Response.AppendHeader "content-disposition", "attachment: filename=TestExcelFile.xls"
Some details on HTML in excel