Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to export GridView to pdf in .net

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 172
    Comment on it

    Hello all,

    In asp.net, to show data in tabular form we use GridView Control, in which we can bind our database table directly and at time we need to export the GridView in from of Excel or PDF file format.

    To export Grid to PDF, we have following block of code:

    In ASPX we have:

    <table class="heading" style="width: 60%; margin: 0 auto;">
                   <tr>
                        <td>
                            <asp:GridView ID="grdpatient" runat="server" CssClass="mGrid" AutoGenerateColumns="false">
                    <Columns>
                        <asp:TemplateField HeaderText="Sr. No.">
                            <ItemTemplate>
                                <center>
                                <asp:Label ID="lblsr" runat="server" Text='<%#Container.DataItemIndex + 1 %>'></asp:Label>
                               </center>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Employee Name">
                            <ItemTemplate>
                              <asp:Label ID="lblEmpName" runat="server" Text='<%# Eval("EmpName") %>'>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Joining Date">
                            <ItemTemplate>
                              <asp:Label ID="lblDOJ" runat="server" Text='<%# Eval("DOJ" , "{0: dd/MM/yyyy}") %>'>
                            </ItemTemplate>
                        </asp:TemplateField>                    
                        <asp:TemplateField HeaderText="Email Id">
                           <ItemTemplate>
                            <asp:Label ID="lblEmail_Id" runat="server" Text='<%# Eval("Email_Id") %>'>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Department">
                            <ItemTemplate>
                              <asp:Label ID="lblDept_Name" runat="server" Text='<%# Eval("Dept_Name") %>'>
                            </ItemTemplate>
                        <</asp:TemplateField>                      
                    </Columns>
                </asp:GridView>
                        </td>
                   </tr>
                   <tr>
                       <td>
                            <asp:ImageButton ID="btnExportToPdf" runat="server" Text="Export To PDF" OnClick="btnExport_Click"  ImageUrl="~/icons/pdf.png" Style="height: 40px;" />
                        </td>
                   </tr>
                </table>
    

    In C# we have :

    protected void btnExport_Click(object sender, ImageClickEventArgs e)
        {
            exportToPdf();
        }
    
        protected void exportToPdf()
        {
            Response.ContentType = "application/pdf";
            string Date = DateTime.Now.ToString();
            Response.AddHeader("content-disposition", "attachment;filename=Discharge-Patient-Report-of-" + Date + ".pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            grdpatient.HeaderRow.Style.Add("width", "20%");
            grdpatient.HeaderRow.Style.Add("font-size", "12px");
            grdpatient.HeaderRow.Style.Add("background-color", "Gray");
            grdpatient.Style.Add("text-decoration", "none");
            grdpatient.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
            grdpatient.Style.Add("font-size", "10px");
            grdpatient.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();
            for (int j = 0; j < grdpatient.Rows.Count; j++)
            {
                pdfDoc.NewPage();
                htmlparser.Parse(sr);
            }
            pdfDoc.Close();
            Response.Write(pdfDoc);
            Response.End();
        }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: