Friday, July 19, 2013

How to Export Gridview Data to PDF in Asp.net

Introduction: in this article I will explain how we can Export the Gridview Data to PDF in Asp.net
Description:
Data Export feature is unavailable in Asp.net so we use the 3rd party Library. Here we use the iTextSharp DLL and reference. To download iTextSharp DLL Click Here.
Take a new website. Put the downloaded iTextSharp DLL in Bin folder and build the project/website. Add a webform to website and design .aspx page as shown below:

<body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr><td>&nbsp;</td><td align="right">
        <asp:Button ID="Button1" runat="server" Text="Export To PDF"
            onclick="Button1_Click" /></td></tr>
    <tr><td>&nbsp;</td><td> <asp:GridView ID="grdstudent" runat="server" AutoGenerateColumns="false" DataKeyNames="STUDENT_ID" DataSourceID="SqlDataSource1">
        <Columns>
        <asp:BoundField DataField="STUDENT_NAME" HeaderText="STUDENT NAME" />
        <asp:BoundField DataField="STUDENT_ADDRESS" HeaderText="STUDENT ADDRESS" />
        <asp:BoundField DataField="STUDENT_CLASS" HeaderText="STUDENT CLASS" />
        </Columns>
        </asp:GridView></td></tr>
    </table>
      
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:TestBlogConnectionString %>"
            SelectCommand="SELECT * FROM [STUDENT_DETAIL]"></asp:SqlDataSource>
    </div>
    </form>
</body>

Now go to .aspx.cs page and write the below given code:

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.IO;

public override void VerifyRenderingInServerForm(Control control)
    {
       //To add control to form
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=Student'sDetail.pdf");
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter ht = new HtmlTextWriter(sw);
        grdstudent.AllowPaging = false;
        grdstudent.DataBind();
        grdstudent.RenderControl(ht);
        grdstudent.HeaderRow.Style.Add("width", "13%");
        grdstudent.HeaderRow.Style.Add("font-size", "11px");
        grdstudent.Style.Add("text-decoration", "none");
        grdstudent.Style.Add("font-family", "Arial, Helvetica, sans-serif;");
        grdstudent.Style.Add("font-size", "10px");
        StringReader sr = new StringReader(sw.ToString());
        Document PDF = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(PDF);
        PdfWriter.GetInstance(PDF, Response.OutputStream);
        PDF.Open();
        htmlparser.Parse(sr);
        PDF.Close();
        Response.Write(PDF);
        Response.End();
    }

In VB (.aspx.vb)

Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparser
Imports System.IO

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)
        'To add control to form
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.ContentType = "application/pdf"
        Response.AddHeader("content-disposition", "attachment;filename=Student'sDetail.pdf")
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Dim sw As New StringWriter()
        Dim ht As New HtmlTextWriter(sw)
        grdstudent.AllowPaging = False
        grdstudent.DataBind()
        grdstudent.RenderControl(ht)
        grdstudent.HeaderRow.Style.Add("width", "13%")
        grdstudent.HeaderRow.Style.Add("font-size", "11px")
        grdstudent.Style.Add("text-decoration", "none")
        grdstudent.Style.Add("font-family", "Arial, Helvetica, sans-serif;")
        grdstudent.Style.Add("font-size", "10px")
        Dim sr As New StringReader(sw.ToString())
        Dim PDF As New Document(PageSize.A2, 7.0F, 7.0F, 7.0F, 0.0F)
        Dim htmlparser As New HTMLWorker(PDF)
        PdfWriter.GetInstance(PDF, Response.OutputStream)
        PDF.Open()
        htmlparser.Parse(sr)
        PDF.Close()
        Response.Write(PDF)
        Response.[End]()

    End Sub

If you get any error "Control 'grdstudent' of type 'GridView' must be placed inside a form tag with runat=server" read this article:

http://articlemirror.blogspot.in/2013/07/control-grdstudent-of-type-gridview.html

No comments:

Post a Comment