Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello, please help me to export a panel with controls to Excel or PDF; when i try ot export with my code just appear an error.

try
{
pnl.EnableViewState = false;
string attachment = "attachment; filename=" + fileName + ".xls"; //Setting the attachment name.
HttpContext.Current.Response.ClearContent(); //clears all content output from the buffer stream.
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
using (HtmlForm frm = new HtmlForm())
{
Page.EnableViewState = false;
frm.Attributes["runat"] = "server";

pnl.Parent.Controls.Add(frm);
frm.Controls.Add(pnl);
frm.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}

}
}
}
catch (Exception ex)
{
throw ex;
}

}


Regards
Posted
Comments
Sergey Alexandrovich Kryukov 6-Aug-15 13:12pm    
Your "exception handling" is abusive: this code is equivalent to not handling exceptions at all, which would be the best option.
—SA
Richard Deeming 6-Aug-15 14:10pm    
Are we supposed to guess what the error is?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900