Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

asp.net

excel

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.

like image 309
Rifat Jahan Azad Avatar asked Oct 27 '25 10:10

Rifat Jahan Azad


1 Answers

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.

like image 69
Md. Tahmidul Abedin Avatar answered Oct 29 '25 00:10

Md. Tahmidul Abedin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!