<div style="float:right;width:auto;" Id="divExport" runat="server" > <asp:ImageButton ID="btnWord" ImageUrl="~/images/word_icon.jpg" runat="server" onclick="btnWord_Click"/> <asp:ImageButton ID="btnExcel" ImageUrl="~/images/Excel-icon.png" runat="server" onclick="btnExcel_Click" /> <asp:ImageButton ID="btnPdf" ImageUrl="~/images/pdf_icon.jpeg" runat="server" onclick="btnPdf_Click" /> <asp:ImageButton ID="btnPrint" ImageUrl="~/images/print.gif" runat="server" /> </div>
protected void btnWord_Click(object sender, ImageClickEventArgs e) { try { Response.Clear(); Response.Buffer = true; Response.Charset = ""; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = "application/vnd.word"; Response.AddHeader("content-disposition", "attachment;filename=EEMS_Report.doc"); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); divExport.RenderControl(hw); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } catch (Exception ex) { throw ex; } } protected void btnExcel_Click(object sender, ImageClickEventArgs e) { try { Response.Clear(); Response.Buffer = true; Response.ClearContent(); Response.ContentType = "application/vnd.ms-excel"; string attachment = "attachment; filename=excel.xls"; Response.AddHeader("content-disposition", attachment); StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); divExport.RenderControl(htw); Response.Write(sw.ToString()); Response.End(); } catch (Exception ex) { throw ex; } } protected void btnPdf_Click(object sender, ImageClickEventArgs e) { try { Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf"); Response.Cache.SetCacheability(HttpCacheability.NoCache); StringWriter sw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(sw); divExport.RenderControl(hw); StringReader sr = new StringReader(sw.ToString()); iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(PageSize.A2); HTMLWorker htmlparser = new HTMLWorker(pdfDoc); PdfWriter.GetInstance(pdfDoc, Response.OutputStream); pdfDoc.Open(); htmlparser.Parse(sr); pdfDoc.Close(); Response.Write(pdfDoc); Response.End(); } catch (Exception ex) { throw ex; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)