.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to export data of grid view into an excel sheet with grid lines.

Posted By:Ashutosh Jha       Posted Date: January 31, 2014    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

In this article I will explain how to export a web page that contain data in a grid view into an excel sheet. Also here I have taken care of the grid lines in the excel sheet. You can use the Gridlines property of the Table to show or hide the Gridlines.
 

In this article I will explain how to export a web page that contain data in a grid view into an excel sheet. Also here I have taken care of the grid lines in the excel sheet. You can use the Gridlines property of the Table to show or hide the Gridlines.



//  Create a table to contain the grid



Table table = new Table();



//  include the gridline settings



table.GridLines = gv.GridLines;



aspx.cs page- make a class and write the below code in that class and then inherit this class to the main class.



using System;



using System.Data;



using System.Configuration;



using System.IO;



using System.Web;



using System.Web.Security;



using System.Web.UI;



using System.Web.UI.WebControls;



using System.Web.UI.WebControls.WebParts;



using System.Web.UI.HtmlControls;



 

public class GridViewExportUtil



{



    public static void Export(string fileName, GridView gv)



    {



        HttpContext.Current.Response.Clear();



        HttpContext.Current.Response.AddHeader(



            "content-disposition", string.Format("attachment; filename={0}", fileName));



        HttpContext.Current.Response.ContentType = "application/ms-excel";



 



        using (StringWriter sw = new StringWriter())



        {



            using (HtmlTextWriter htw = new HtmlTextWriter(sw))



            {



                //  Create a table to contain the grid



                Table table = new Table();



 



                //  include the gridline settings



                table.GridLines = gv.GridLines;



 



                //  add the header row to the table



                if (gv.HeaderRow != null)



                {



                    GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);



                    table.Rows.Add(gv.HeaderRow);



                }



 



                //  add each of the data rows to the table



                foreach (GridViewRow row in gv.Rows)



                {



                    GridViewExportUtil.PrepareControlForExport(row);



                    table.Rows.Add(row);



                }



 



                //  add the footer row to the table



                if (gv.FooterRow != null)



                {



                    GridViewExportUtil.PrepareControlForExport(gv.FooterRow);



                    table.Rows.Add(gv.FooterRow);



                }



 



                //  render the table into the htmlwriter



                table.RenderControl(htw);



 



                //  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 CheckBox)



            {



                control.Controls.Remove(current);



                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));



            }



 



            if (current.HasControls())



            {



                GridViewExportUtil.PrepareControlForExport(current);



            }



        }



    }



}



And then on button click event write the below code- 

protected void BtnExportGrid_Click(object sender, EventArgs args)
{
    if (this.rdoBtnListExportOptions.SelectedIndex == 1)
    {
      
        this.gvCustomers.AllowPaging = false;
        this.gvCustomers.DataBind();
    }

 



 



 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend