65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.33/5 (5 votes)

May 7, 2012

CPOL
viewsIcon

66782

downloadIcon

5633

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(); 
        }
    }
}