Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8571059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:41:59+00:00 2026-06-11T18:41:59+00:00

I have following code that I am using for export to excel of a

  • 0

I have following code that I am using for export to excel of a gridview. I am adding the gridview rows into System.Web.UI.WebControls.Table. Now, I need to apply background color to the header and data rows in the exported excel (There are two header rows).

I tired the following. It is not providing the desired result.

Issues of current solution

  1. One header row does not have background color
  2. Coloring is applied to cells that does not have data (cells “H”, “I”, etc.)

How can we correct it?

enter image description here

Note: I am trying to learn the export feature. So, please don’t suggest to use any third party controls. I am just exploring all features of this approach.

I am adding the header grouping to the original gridview using following code.

protected void gvCustomers_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        System.Text.StringBuilder sbNewHeader = new StringBuilder();
        sbNewHeader.AppendFormat("&nbsp;</th>" +
            "<th colspan='2' class='tableColGroupAssociate'>Associate Info <a href='#' class='associateHide'> Hide </a> </th>" +
            "<th colspan='2' class='tableColGroupTransaction'>Financial Info <a href='#' class='financialHide'> Hide </a> </th>" +
            "<th colspan='2' class='tableColGroupDailyTax'>Tax Info <a href='#' class='dailyTaxHide'> Hide </a> </th>"
            + "</tr>");
        sbNewHeader.AppendFormat("<tr class='{0}'><th>{1}", this.gvCustomers.HeaderStyle.CssClass, e.Row.Cells[0].Text);
        e.Row.Cells[0].Text = sbNewHeader.ToString();
    }
}

Complete Code

public static void Export(GridView gv)
{
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "MyExcelFile.xls"));
    HttpContext.Current.Response.ContentType = "application/ms-excel";

    using (StringWriter stringWriter = new StringWriter())
    {
        using (HtmlTextWriter tetxWriter = new HtmlTextWriter(stringWriter))
        {

            System.Web.UI.WebControls.Table tableControl = new Table();
            tableControl.GridLines = gv.GridLines;

            //Before the next step - we can remove any controls inside the gridview and replace with literal control

            //  Add the header row to the table
            if (gv.HeaderRow != null)
            {
                TableRow tableRow = gv.HeaderRow;
                tableRow.Style[System.Web.UI.HtmlTextWriterStyle.BackgroundColor] = "Orange";
                tableControl.Rows.Add(gv.HeaderRow);
            }

            //  Add each of the data rows to the table
            foreach (GridViewRow row in gv.Rows)
            {
                TableRow tableRow = row;
                tableRow.Style[System.Web.UI.HtmlTextWriterStyle.BackgroundColor] = "Yellow";
                tableControl.Rows.Add(row);
            }


            //  Render the table into the htmlwriter
            tableControl.RenderControl(tetxWriter);

            //  Render the htmlwriter into the response
            HttpContext.Current.Response.Write(stringWriter.ToString());
            HttpContext.Current.Response.End();


        }
    }
}

EDIT

Based on comment from Ankit, I tried the following; still the result is not as expected.

            if (gv.HeaderRow != null)
            {
                TableRow tableRow = gv.HeaderRow;

                foreach (TableCell cell in tableRow.Cells)
                {
                    cell.Style[System.Web.UI.HtmlTextWriterStyle.BackgroundColor] = "Orange";
                }
                tableControl.Rows.Add(gv.HeaderRow);
            }

enter image description here


  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T18:42:01+00:00Added an answer on June 11, 2026 at 6:42 pm

    I figured a way to do it… For the benefit of others I will post it here:

    References:

    1. ASP.NET Excel export encoding problem

    enter image description here

    Other Note: When a new Table is created TableSection can be used to define header

     newRow.TableSection = TableRowSection.TableHeader;
    

    CODE

    //Changed the logic used for adding dynamic header for HTML rendering:

    protected void gvCustomers_RowCreated(object sender, GridViewRowEventArgs e)
    {
    
        if (e.Row.RowType == DataControlRowType.Header)
        {
            GridViewRow newHeaderRow = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
    
            TableCell cell1 = new TableHeaderCell();
            cell1.ColumnSpan = 1; //e.Row.Cells.Count;
            cell1.Text = "";
    
            TableCell cell2 = new TableCell();
            cell2.ColumnSpan = 2;
            cell2.Text = "One";
    
            TableCell cell3 = new TableCell();
            cell3.ColumnSpan = 2;
            cell3.Text = "Two";
    
            TableCell cell4 = new TableCell();
            cell4.ColumnSpan = 2;
            cell4.Text = "Three";
    
            newHeaderRow.Cells.Add(cell1);
            newHeaderRow.Cells.Add(cell2);
            newHeaderRow.Cells.Add(cell3);
            newHeaderRow.Cells.Add(cell4);
    
            ((GridView)sender).Controls[0].Controls.AddAt(0, newHeaderRow);
        }
    
        if (e.Row.RowType == DataControlRowType.Header)
        {
    
            //System.Text.StringBuilder sbNewHeader = new StringBuilder();
            //sbNewHeader.AppendFormat("&nbsp;</th>" +
            //    "<th colspan='2' class='tableColGroupAssociate'>Associate Info <a href='#' class='associateHide'> Hide </a> </th>" +
            //    "<th colspan='2' class='tableColGroupTransaction'>Financial Info <a href='#' class='financialHide'> Hide </a> </th>" +
            //    "<th colspan='2' class='tableColGroupDailyTax'>Tax Info <a href='#' class='dailyTaxHide'> Hide </a> </th>"
            //    + "</tr>");
            //sbNewHeader.AppendFormat("<tr class='{0}'><th>{1}", this.gvCustomers.HeaderStyle.CssClass, e.Row.Cells[0].Text);
            //e.Row.Cells[0].Text = sbNewHeader.ToString();
    
    
        }
    
    }
    

    //Export Logic – Applied similar logic once again

    public static void Export(GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "MyExcelFile.xls"));
        HttpContext.Current.Response.ContentType = "application/ms-excel";
    
    
        //Response.ContentEncoding = System.Text.Encoding.Unicode;
        //Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
    
    
        using (StringWriter stringWriter = new StringWriter())
        {
            using (HtmlTextWriter tetxWriter = new HtmlTextWriter(stringWriter))
            {
    
                System.Web.UI.WebControls.Table tableControl = new Table();
                tableControl.GridLines = gv.GridLines;
    
                //  Add the header row to the table
                if (gv.HeaderRow != null)
                {
                    ReplaceControlForExport(gv.HeaderRow);
    
    
                    #region Dynamic Frrst Header Row
    
                    TableRow newRow = new TableRow();
    
                    TableCell cell1 = new TableHeaderCell();
                    cell1.ColumnSpan = 1; 
                    cell1.Text = "";
    
                    TableCell cell2 = new TableCell();
                    cell2.ColumnSpan = 2;
                    cell2.Text = "One";
    
                    TableCell cell3 = new TableCell();
                    cell3.ColumnSpan = 2;
                    cell3.Text = "Two";
    
                    TableCell cell4 = new TableCell();
                    cell4.ColumnSpan = 2;
                    cell4.Text = "Three";
    
                    newRow.Cells.Add(cell1);
                    newRow.Cells.Add(cell2);
                    newRow.Cells.Add(cell3);
                    newRow.Cells.Add(cell4);
    
                    foreach (TableCell cell in newRow.Cells)
                    {
                        cell.Style[System.Web.UI.HtmlTextWriterStyle.BackgroundColor] = "Purple";
                    }
                    tableControl.Rows.Add(newRow);
    
                    #endregion 
    
                    TableRow originalHeaderRow = gv.HeaderRow;
                    foreach (TableCell cell in originalHeaderRow.Cells)
                    {
                        cell.Style[System.Web.UI.HtmlTextWriterStyle.BackgroundColor] = "Orange";
                    }
                    tableControl.Rows.Add(originalHeaderRow);
                }
    
    
                //  Add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    ReplaceControlForExport(row);
    
                    TableRow tableRow = row;
                    foreach (TableCell cell in tableRow.Cells)
                    {
                        cell.Style[System.Web.UI.HtmlTextWriterStyle.BackgroundColor] = "Yellow";
                    }
                    tableControl.Rows.Add(row);
                }
    
    
                //  Render the table into the htmlwriter
                tableControl.RenderControl(tetxWriter);
    
                //  Render the htmlwriter into the response
                HttpContext.Current.Response.Write(stringWriter.ToString());
                HttpContext.Current.Response.End();
    
    
            }
        }
      }
    
     private static void ReplaceControlForExport(Control mainControlElement)
     {
        for (int i = 0; i < mainControlElement.Controls.Count; i++)
        {
            Control currentControl = mainControlElement.Controls[i];
    
            if (currentControl is LinkButton)
            {
                mainControlElement.Controls.Remove(currentControl);
                mainControlElement.Controls.AddAt(i, new LiteralControl((currentControl as LinkButton).Text));
            }
            else if (currentControl is ImageButton)
            {
                mainControlElement.Controls.Remove(currentControl);
                mainControlElement.Controls.AddAt(i, new LiteralControl((currentControl as ImageButton).AlternateText));
            }
            else if (currentControl is HyperLink)
            {
                mainControlElement.Controls.Remove(currentControl);
                mainControlElement.Controls.AddAt(i, new LiteralControl((currentControl as HyperLink).Text));
            }
            else if (currentControl is DropDownList)
            {
                mainControlElement.Controls.Remove(currentControl);
                mainControlElement.Controls.AddAt(i, new LiteralControl((currentControl as DropDownList).SelectedItem.Text));
            }
            else if (currentControl is CheckBox)
            {
                mainControlElement.Controls.Remove(currentControl);
                mainControlElement.Controls.AddAt(i, new LiteralControl((currentControl as CheckBox).Checked ? "True" : "False"));
            }
    
            //Recursive Call
            if (currentControl.HasControls())
            {
                ReplaceControlForExport(currentControl);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code that works without using throw however when I use
I am using Lazarus v0.9.30 (32 bit compiler). I have the following code that
I have the following block of jQuery code that I'm using to copy some
I have a combobox that is bound to an enum using the following code:
I'm using SDL with FASM, and have code that's minimally like the following: format
I have following code that I am compiling in a .NET 4.0 project namespace
I have following code that does not work due to a being a value
I have following code that does not work: I never get to goToFoodDetail .
I have following code snippet that i use to compile class at the run
I have the following code that has @item.ID from razor: <a href=# id=deleteitem(@item.ID)>Delete</a> When

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.