Many times we would like to export our page as an Excel sheet, Word doc or PDF file. This can be simply achieved by changing the ContentType of the Response object and overriding the RenderControl method of the control to be exported:

Page Load(){ //bind data to data bound controls and do other stuff

Response.Clear();

//this clears the Response of any headers or previous output Response.Buffer = true; //make sure that the entire output is rendered simultaneously

"application/vnd.ms-excel";StringWriter stringWriter = newStringWriter(); //System.IO namespace should be used

HtmlTextWriter htmlTextWriter = newHtmlTextWriter(stringWriter);

//////Render the entire Page control in the HtmlTextWriter object///We can render individual controls also, like a DataGrid to be///exported in custom format (excel, word etc)///this.RenderControl(htmlTextWriter);Response.Write(stringWriter.ToString());Response.End();} //end page load

For exporting ASP.NET pages as PDF files, you need to know the Adobe PDF specs for generating the PDf correctly. There is already a wonderful component which does the same and its free! See this article along with sample source code on how to export a page to PDF:

Now what namespace for Document. - shekhar kumar replied to Binny ch on 11-Sep-08 05:41 AM

Server Error in '/UnaidsMisNew' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0246: The type or namespace name 'Document' could not be found (are you missing a using directive or an assembly reference?)
Source Error:
Line 186: if (GridView1.Visible == true)
Line 187: {
Line 188: Document document = new Document(PageSize.A4, 0, 0, 50, 50);
Line 189: System.IO.MemoryStream msReport = new System.IO.MemoryStream();
Line 190:
this is the error I am getting

Thanks - shekhar kumar replied to Binny ch on 11-Sep-08 07:14 AM

Exporting to PDF - Lokesh M replied to Sagar P on 12-Sep-08 01:16 AM

Hi, Below code is working fine.. all the data exported to pdf from Gridview/Table, and there are 4 columns But for first column occupies half of the document, rests are displaying in another half. Same problem if i export html table also..