Click here to Skip to main content
11,646,594 members (76,152 online)
Click here to Skip to main content

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

, 7 May 2012 CPOL 39.9K 4.5K 9
Rate this:
Please Sign up or sign in to vote.
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.

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)

Share

About the Author

Vivek_Sharma
Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionGetting an Error While Exporting a Grid Data To PDF Pin
siva Prasad Paruchuri18-Feb-14 1:03
membersiva Prasad Paruchuri18-Feb-14 1:03 
Questionit giv me an error Pin
hasan_sas526-Aug-13 2:32
memberhasan_sas526-Aug-13 2:32 
GeneralMy vote of 1 Pin
Juan Pablo G.C.8-May-12 21:53
memberJuan Pablo G.C.8-May-12 21:53 
QuestionNot an article Pin
OriginalGriff7-May-12 8:16
mvpOriginalGriff7-May-12 8:16 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150731.1 | Last Updated 7 May 2012
Article Copyright 2012 by Vivek_Sharma
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid