Click here to Skip to main content
15,889,852 members
Articles / Web Development / ASP.NET
Tip/Trick

How to convert GridView data to Excel, PDF, Word file using C#

Rate me:
Please Sign up or sign in to vote.
2.33/5 (5 votes)
7 May 2012CPOL 66.2K   5.6K   10   5
Convert GridView data to Excel, PDF, Word file using C#

Introduction

This code provides a way to convert your GridView data to an Excel, Doc, or PDF file as per your requirments.

Using the code

This is the code snippet which you have to use, or download the above zip file.

C#
protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        SqlConnection cn = new SqlConnection(
             System.Configuration.ConfigurationManager.ConnectionStrings["Connect"].ConnectionString);
        cn.Open();
        SqlCommand cmd = new SqlCommand("Select * from Show", cn);
        SqlDataReader dtr = cmd.ExecuteReader();

        if (dtr.HasRows)
        {
            dtr.Read();
            gv.DataSource = dtr;
            gv.DataBind();
        }
        cn.Close();
    }
}

//On Click Event

protected void btnword_Click(object sender, EventArgs e)
{
    if(DropDownList1.SelectedIndex!=0)
    {
        if (DropDownList1.SelectedValue=="Excel")
        {
            string attachment = "attachment; filename=Export.xls";
            Response.ClearContent();
            Response.AddHeader("content-disposition", attachment);
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            HtmlForm frm = new HtmlForm();
            gv.Parent.Controls.Add(frm);
            frm.Attributes["runat"] = "server";
            frm.Controls.Add(gv);
            frm.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
        }
        if(DropDownList1.SelectedValue=="Word")
        {
            Response.AddHeader("content-disposition", "attachment;filename=Export.doc");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.word";
  
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
  
            HtmlForm frm = new HtmlForm();
            gv.Parent.Controls.Add(frm);
            frm.Attributes["runat"] = "server";
            frm.Controls.Add(gv);
            frm.RenderControl(htmlWrite);
       
            Response.Write(stringWrite.ToString());
            Response.End();
        }
        if(DropDownList1.SelectedValue=="PDF")
        {
            Response.ContentType = "application/pdf";
            Response.AddHeader("content-disposition", "attachment;filename=Export.pdf");
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            HtmlForm frm = new HtmlForm();
            gv.Parent.Controls.Add(frm);
            frm.Attributes["runat"] = "server";
            frm.Controls.Add(gv);
            frm.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(); 
        }
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionExport gridview to PDF and autogenerate property true in Arabic words Pin
Kashif Ansari 12311-Jan-17 4:59
Kashif Ansari 12311-Jan-17 4:59 
QuestionGetting an Error While Exporting a Grid Data To PDF Pin
siva Prasad Paruchuri18-Feb-14 1:03
siva Prasad Paruchuri18-Feb-14 1:03 
Questionit giv me an error Pin
hasan_sas526-Aug-13 2:32
hasan_sas526-Aug-13 2:32 
GeneralMy vote of 1 Pin
Juan Pablo G.C.8-May-12 21:53
Juan Pablo G.C.8-May-12 21:53 
It would better if you simply tell us that exists iTextSharp
QuestionNot an article Pin
OriginalGriff7-May-12 8:16
mveOriginalGriff7-May-12 8:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.