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
Showing posts with label Excel. Show all posts
Showing posts with label Excel. Show all posts
Tuesday, 3 August 2010
Friday, 16 July 2010
DISP_E_OVERFLOW
When working with Interop Excel, is better to read values from a given cell by using mySheet.Range("A2").Value2 instead of mySeet.Range("A2").Value. This is because when a cell is not displaying the whole content, for instance "######" the property Value will throw the exception DISP_E_OVERFLOW, but when you are using Value2 you will get the actual value inside the cell rather than the value displayed on the screen.
Subscribe to:
Posts (Atom)