Export tables,
div, Grids and any to Excel and PDF in asp.net c#
àWhile
exporting these designs with out images
it will working fine
à If
it having images means you need to give the absolute path for the image URL then
only it will work
Check this
blog
Excel Export
protected void
imgexcel_Click(object sender, ImageClickEventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition",
"attachment;filename=Suppliers.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter
htmlWrite = new HtmlTextWriter(stringWrite);
tblrpt.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
//Response.Clear();
//Response.Buffer = true;
//Response.AddHeader("content-disposition",
//
"attachment;filename=GridViewExport.doc");
//Response.Charset = "";
//Response.ContentType = "application/vnd.ms-word
";
//StringWriter sw = new StringWriter();
//HtmlTextWriter hw = new HtmlTextWriter(sw);
//imgReportHeading.Width = 160;
//imgReportHeading.Height = 42;
//tblrpt.RenderControl(hw);
//Response.Output.Write(sw.ToString());
//Response.Flush();
//Response.End();
}
Pdf
Export
protected void imgpdf_Click(object sender, ImageClickEventArgs
e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
"attachment;filename=GridViewExport.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new
StringWriter();
HtmlTextWriter hw = new
HtmlTextWriter(sw);
tblrpt.RenderControl(hw);
StringReader sr = new
StringReader(sw.ToString());
Document pdfDoc = new
Document(PageSize.A4,
10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new
HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc,
Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}