Html / Aspx Export to Excel

G

Guest

for exporting to excel just call the function below in sample code and pass
the page object from an aspx and the aspx page contents are rendered in
excel..

make control.visible = false if you dont want those controls to be exported
to excel.

the same function can be overloaded to pass say a datagrid (in most search
screens we need results only)also.



// For Direct Exporting From HTML to excel
// Call this function on Button Click
public static void HtmlExportToExcel(Page PageObject)

{

HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Clear();

HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

HttpContext.Current.Response.ContentEncoding =
System.Text.Encoding.Default;
HttpContext.Current.Response.AddHeader("content-disposition",
"attachment; filename=RMCExport-" +
System.DateTime.Now.ToString("MMMddyyyy-hhmmtt") + ".xls");
HttpContext.Current.Response.Charset = string.Empty ;

PageObject.EnableViewState = false;

StringWriter tw = new StringWriter();

HtmlTextWriter hw = new HtmlTextWriter(tw);

//DataGrid dg = new DataGrid();

//dg.DataSource = SourceDataTable;

//dg.DataBind();
PageObject.RenderControl (hw);
//dgrSearchResults.RenderControl(hw);

HttpContext.Current.Response.Write(tw.ToString());

HttpContext.Current.Response.Flush() ;

HttpContext.Current.Response.Close();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top