Rifat Jahan Azad
Rifat Jahan Azad

Reputation: 43

How to export gridview data of multiple pages into an excel document?

I have used gridview in my C# asp.net form application and I have enabled "AllowPaging" and now whenever I want to convert it to excel only the first page is shown. The conversion is done using "HtmlTextWriter" and "StringWriter".

-Thanks in advance.

Upvotes: 4

Views: 1692

Answers (2)

Md. Tahmidul Abedin
Md. Tahmidul Abedin

Reputation: 136

To export excel from gridview you can use the following class in your project.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;

namespace gride_edit
{
    public class GridToExcel
    {
        public static void ExportToExcel(string strFileName, GridView gv)
        {
            using (StringWriter sw = new StringWriter())
            {
                using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                {

                    //  Create a form to contain the grid  
                    Table table = new Table();

                    //  add the header row to the table  
                    if (gv.HeaderRow != null)
                    {
                        PrepareControlForExport(gv.HeaderRow);
                        table.Rows.Add(gv.HeaderRow);
                    }

                    //  add each of the data rows to the table  
                    foreach (GridViewRow row in gv.Rows)
                    {
                        PrepareControlForExport(row);
                        row.Style["background-color"] = "#FFFBD6";
                        table.Rows.Add(row);
                    }

                    //  add the footer row to the table  
                    if (gv.FooterRow != null)
                    {
                        PrepareControlForExport(gv.FooterRow);
                        table.Rows.Add(gv.FooterRow);
                    }

                    //  render the table into the htmlwriter  
                    table.RenderControl(htw);

                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", strFileName));
                    HttpContext.Current.Response.ContentType = "application/ms-excel";
                    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);

                    //render the htmlwriter into the response  
                    HttpContext.Current.Response.Write(sw.ToString());
                    HttpContext.Current.Response.End();
                }
            }
        }

        private static void PrepareControlForExport(Control control)
        {
            for (int i = 0; i < control.Controls.Count; i++)
            {
                Control current = control.Controls[i];
                if (current is LinkButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
                }
                else if (current is ImageButton)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
                }
                else if (current is HyperLink)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
                }
                else if (current is DropDownList)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
                }
                else if (current is HiddenField)
                {
                    control.Controls.Remove(current);
                }
                else if (current is CheckBox)
                {
                    control.Controls.Remove(current);
                    control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
                }

                if (current.HasControls())
                {
                    PrepareControlForExport(current);
                }
            }
        }  
    }
}

and can call this function to export.

GridToExcel.ExportToExcel("Report" + DateTime.Now.ToString("yyyyMMddhhmmss").ToString() + ".xls", GridView1);

And for export one excel from multiple paged gridview, just use these line before calling the convert function "ExportToExcel()".

GridView1.AllowPaging = false;
bindGridView();

I got the "GridToExcel" class from internet. It worked for me, hope it will work for you too. Please let me know.

Upvotes: 2

Monah
Monah

Reputation: 6784

In general you can use the following function

 public void Export(HttpResponseBase Response, object data)
 {
        var gridItem = new System.Web.UI.WebControls.GridView();
        gridItem.DataSource = data;
        gridItem.DataBind();
        StringWriter writer = new StringWriter();
        HtmlTextWriter html = new HtmlTextWriter(writer);

        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=result.xls");
        Response.ContentType = "application/excel";

        gridItem.RenderControl(html);
        Response.Write(writer.ToString());
        Response.End();
 }

and you can call this function as

var details = /* your query result goes here */ ;
Export(Response , details );

hope it helps you

Upvotes: 0

Related Questions