I want to export data to excel both in .xls and .xlsx format in asp.net
MVC without using any third party control.
I tried using following code but it doesn’t support .xlsx format.
Response.AddHeader("Content-Disposition", "attachment;
filename=test.xlsx");
Response.ContentType = "application/ms-excel";
Response.Write(sw.ToString());
Response.End();
Pls give me solution.
Besides Open XML SDK 2.0 described by @amurra, you can try OpenExcel. It is excellent library and is quite fast.
A quick and dirty option is to put all your data in a table and write that table only to response stream with the headers you are trying already, like:
I’ve not used this hack but have seen it used in some application. When opening it Excel gives user a warning that the data is not excel format, but opens it successfully.