How to save the contents of a GridView to an excel spreadsheet

Posted on 2010-01-07
Last Modified: 2012-05-08
I am trying to save my GridView data as an Excel file.

Here's my code

            Response.AddHeader("content-disposition", "attachment;filename=Report.xls");
           // Response.ContentType = "application/octet-stream";

            // If you want the option to open the Excel file without saving then
            // comment out the line below
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

It doesn't work and I get a I get an error about Managed Pipelines. After changing it to Integrated, I still cant get it to work.

Question by:fantasylan
    1 Comment
    LVL 1

    Accepted Solution


            /// <summary>
            /// Export GridView data to Excel.
            /// </summary>
            /// <param name="grdView">GridView control to export.</param>
            /// <param name="filename">Filename of excel spreadsheet.</param>
            /// <param name="excludedColumnList">ArrayList of columns to exlude.</param>
         protected void ExportGridView(GridView grdView, string filename, ArrayList excludedColumnList)
                // Clear response content & headers

                // Add header
                Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls");

                Response.Charset = string.Empty;
                Response.ContentType = "application/vnd.xls";

                // Create stringWriter
                System.IO.StringWriter stringWrite = new System.IO.StringWriter();

                 // Create HtmlTextWriter
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

                // Remove controls from Column Headers
                if (grdView.HeaderRow != null && grdView.HeaderRow.Cells != null)
                    for (int ct = 0; ct < grdView.HeaderRow.Cells.Count; ct++)
                        // Save initial text if found
                        string headerText = grdView.HeaderRow.Cells[ct].Text;

                        // Check for controls in header
                        if (grdView.HeaderRow.Cells[ct].HasControls())
                            // Check for link button
                            if (grdView.HeaderRow.Cells[ct].Controls[0].GetType().ToString() == "System.Web.UI.WebControls.DataControlLinkButton")
                                // link button found, get text
                                headerText = ((LinkButton)grdView.HeaderRow.Cells[ct].Controls[0]).Text;

                            // Remove controls from header

                        // Reassign header text
                        grdView.HeaderRow.Cells[ct].Text = headerText;

                // Remove footer
                if (grdView.FooterRow != null)
                    grdView.FooterRow.Visible = false;

                // Remove unwanted columns (header text listed in removeColumnList arraylist)
                foreach (DataControlField field in grdView.Columns)
                    if (excludedColumnList.Contains(field.HeaderText))
                        field.Visible = false;

                // Call gridview's renderControl

                // Write Response to browser


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    This video discusses moving either the default database or any database to a new volume.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now