Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a gridview which has multiple headers added during run time. I want to export the gridview data to excel with the same formating. But while I try to do so the color of the multiple header rows continues beyond the table area in output excel file, which is not desired. The code used for exporting data is as follows. Please help to solve this.
Thanks.

What I have tried:

<code>protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            gdView.HeaderRow.BackColor = Color.White;
            foreach (TableCell hcell in gdView.HeaderRow.Cells)
            {
                hcell.BackColor = Color.White;
            }
            foreach (GridViewRow row in gdView.Rows)
            {
                {
                    row.BackColor = Color.White;
                    foreach (TableCell cell in row.Cells)
                    {
                        cell.CssClass = "textmode";
                    }
                }
            }
            gdView.RenderControl(hw);
            //style to format numbers to string
            string style = @"<style> .textmode { } </style>";
            Response.Write(style);
            Response.Output.Write(sw.ToString());
            Response.Flush();
            Response.End();
        }
    }
    public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
    {
        // controller   
    }</code>
Posted
Updated 3-Jul-17 9:58am
v2
Comments
Maciej Los 3-Jul-17 15:33pm    
All you need to do is to debug your programme.
Richard Deeming 4-Jul-17 12:00pm    
NB: You're not generating an Excel file. You're generating an HTML file, and asking Excel to import it. As a result, you'll have very little control over the formatting.

If you want complete control of the formatting, you'll need to generate a real Excel file. There are various free libraries which will let you do that. For example:

* EPPlus[^];
* ClosedXML[^];
* The OpenXML SDK[^];

1 solution

I could finally solve the issue by modifying the code as below:

protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            using (StringWriter sw = new StringWriter())
            {
                HtmlTextWriter hw = new HtmlTextWriter(sw);
                for (int x = 0; x <= 3; x++)
                {
                    GridViewRow rows = (GridViewRow)gdView.HeaderRow.Parent.Controls[x];
                    rows.BackColor = Color.White;
                    rows.Height = 15;
                    for (int i = 0; i <= rows.Cells.Count - 1; i++)
                    {
                        rows.Cells[i].BackColor = Color.Maroon;
                    }
                } 
                foreach (GridViewRow row in gdView.Rows)
                {
                    row.BackColor = Color.White;
                    foreach (TableCell cell in row.Cells)
                    {
                        cell.VerticalAlign = VerticalAlign.Middle;
                        cell.CssClass = "textmode";
                    }
                }
                gdView.RenderControl(hw);
                //style to format numbers to string
                string style = @"<style> .textmode { } </style>";
                Response.Write(style);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }
        }
        public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
        {
            // controller   
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900