Tuesday, 22 October 2013

How to Export Gridview data to PDF File in ASP.NET using C#/VB.NET

 Hi friends, in this article I will explain about  How to Export Gridview data to PDF File in ASP.NET using C#/VB.NET.
In ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to Export to PDF in ASP.NET using C#/VB.NET</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="Gv_Data" runat="server">
        </asp:GridView>
        <asp:Button ID="btnPdf" runat="server" Text="Button" onclick="btnPdf_Click"
            style="height: 26px" />
    </div>
    </form>
</body>
</html>


In C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.IO;
public partial class ImportToPdf : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();

    }
    protected void BindData()
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        SqlCommand cmd = new SqlCommand("SELECT * FROM STUDENTS ORDER BY Firstname ASC", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        Gv_Data.DataSource = ds.Tables[0];
        Gv_Data.DataBind();      
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Verifies that the control is rendered */
    }
    protected void btnPdf_Click(object sender, EventArgs e)
    {
        Response.ContentType = "application/pdf";
        string attachment = string.Empty;
        attachment = "attachment; filename=UserData_" + DateTime.Now.ToString("ddMMyyyy'_'HHmmss") + ".pdf";
        Response.AddHeader("content-disposition", attachment);
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter str_w = new StringWriter();
        HtmlTextWriter htm_w = new HtmlTextWriter(str_w);
        Gv_Data.AllowPaging = false;
        Gv_Data.DataBind();
        Gv_Data.RenderControl(htm_w);
        StringReader str_r = new StringReader(str_w.ToString());
        Document pdf_Doc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
        HTMLWorker htmlparser = new HTMLWorker(pdf_Doc);
        PdfWriter.GetInstance(pdf_Doc, Response.OutputStream);
        pdf_Doc.Open();
        htmlparser.Parse(str_r);
        pdf_Doc.Close();
        Response.Write(pdf_Doc);
        Response.End();
    }
  
}

In VB.NET:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparser
Imports System.IO
Partial Public Class ImportToPdf
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        BindData()

    End Sub
    Protected Sub BindData()
        Dim con As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("con").ConnectionString)
        Dim cmd As New SqlCommand("SELECT * FROM STUDENTS ORDER BY Firstname ASC", con)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet()
        da.Fill(ds)
        Gv_Data.DataSource = ds.Tables(0)
        Gv_Data.DataBind()
    End Sub
    Public Overrides Sub VerifyRenderingInServerForm(control As Control)
        ' Verifies that the control is rendered

    End Sub
    Protected Sub btnPdf_Click(sender As Object, e As EventArgs)
        Response.ContentType = "application/pdf"
        Dim attachment As String = String.Empty
        attachment = "attachment; filename=UserData_" + DateTime.Now.ToString("ddMMyyyy'_'HHmmss") + ".pdf"
        Response.AddHeader("content-disposition", attachment)
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        Dim str_w As New StringWriter()
        Dim htm_w As New HtmlTextWriter(str_w)
        Gv_Data.AllowPaging = False
        Gv_Data.DataBind()
        Gv_Data.RenderControl(htm_w)
        Dim str_r As New StringReader(str_w.ToString())
        Dim pdf_Doc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
        Dim htmlparser As New HTMLWorker(pdf_Doc)
        PdfWriter.GetInstance(pdf_Doc, Response.OutputStream)
        pdf_Doc.Open()
        htmlparser.Parse(str_r)
        pdf_Doc.Close()
        Response.Write(pdf_Doc)
        Response.[End]()
    End Sub

End ClassImports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports iTextSharp.text.html.simpleparser
Imports System.IO
    Partial Public Class ImportToPdf
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(sender As Object, e As EventArgs)
            BindData()

        End Sub
        Protected Sub BindData()
            Dim con As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("con").ConnectionString)
            Dim cmd As New SqlCommand("SELECT * FROM STUDENTS ORDER BY Firstname ASC", con)
            Dim da As New SqlDataAdapter(cmd)
            Dim ds As New DataSet()
            da.Fill(ds)
            Gv_Data.DataSource = ds.Tables(0)
            Gv_Data.DataBind()
        End Sub
        Public Overrides Sub VerifyRenderingInServerForm(control As Control)
            ' Verifies that the control is rendered

        End Sub
        Protected Sub btnPdf_Click(sender As Object, e As EventArgs)
            Response.ContentType = "application/pdf"
            Dim attachment As String = String.Empty
            attachment = "attachment; filename=UserData_" + DateTime.Now.ToString("ddMMyyyy'_'HHmmss") + ".pdf"
            Response.AddHeader("content-disposition", attachment)
            Response.Cache.SetCacheability(HttpCacheability.NoCache)
            Dim str_w As New StringWriter()
            Dim htm_w As New HtmlTextWriter(str_w)
            Gv_Data.AllowPaging = False
            Gv_Data.DataBind()
            Gv_Data.RenderControl(htm_w)
            Dim str_r As New StringReader(str_w.ToString())
            Dim pdf_Doc As New Document(PageSize.A4, 10.0F, 10.0F, 10.0F, 0.0F)
            Dim htmlparser As New HTMLWorker(pdf_Doc)
            PdfWriter.GetInstance(pdf_Doc, Response.OutputStream)
            pdf_Doc.Open()
            htmlparser.Parse(str_r)
            pdf_Doc.Close()
            Response.Write(pdf_Doc)
            Response.End)
        End Sub
    End Class

The output of the above code as shown in the below figure.

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.