In this post I will explain a simple way to generate excel files out of webforms using mime types.
It is very easy, just follow this steps:
1. In the "@ page" declaration, add these attributes: EnableViewState="false" CodePage="1252". By disabling viewstate you prevent some errors when opening the file with Excel and the CodePage is for allowing unicode characters to be displayed correctly in the worksheet.
2. In the Page_Load handler, type the following line:
Response.ContentType = "application/vnd.ms-excel"
It is important to use this value because others (application/excel and application/ms-excel) does not work always.
3. Do not use CSS files. Since your content will be generated as an Excel file, you cannot link to external files. To allow styles, put all your css in the same page.
That's it, by performing these steps you can now export an aspx page to a Excel file.
Related articles by Zemanta