Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip some column while exporting Grid view in excel?

I have a grid view. Which contains some columns. Suppose, it has 10 columns out of them only 8 columns has header while 2 columns header are empty. Now i am exporting this gridview into excel which contains all 10 columns with 2 columns without name. How can i exclude that 2 empty header columns while exporting GridView into excel. I am using following code for the same:

        protected void btnExportToExcel_Click(object sender, EventArgs e)
        {
            Export("Customers.xls", this.gdvMessages);
        }

        private 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 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);
                        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);

                    //  render the htmlwriter into the response
                    HttpContext.Current.Response.Write(sw.ToString());
                    HttpContext.Current.Response.End();
                }
            }
        }
like image 725
Sandy Avatar asked Nov 28 '11 05:11

Sandy


2 Answers

You could just not include those two columns in your HTML table if you don't need them.... or you could re-write the table when the user press the "Export to Excel" button and not include the columns. You could also have the table plus another table with hidden columns and get the data from it when the user clicks the "Export to Excel" button. Furthermore, you could not have the data shown at all, and when the User loads the page, or click the Button download the data without the columns. Good luck!

EDITED: For this exclusive scenario, you may want to edit your code in the following way:

 //  add the header row to the table
            if (gv.HeaderRow != null)
            {
                TableRow tr = new TableRow();

                foreach (DataControlFieldHeaderCell c in gv.HeaderRow.Cells)
                {
                    if (c.Text != " ") {
                        tr.Cells.Add(new TableCell() { Text = c.Text});
                    }
                }

                PrepareControlForExport(tr);
                table.Rows.Add(tr);
            }

What that portion of the code does is to loop through each cell in the header row, and if it's empty then don't include it. Then add it to the html table. This can be tweaked if wanted so that the rest of the table add a cellspan where these columns belong. If this is not what you need maybe a couple of screenshots with your actual Gridview and the desired output would be ideal.

Good luck!

like image 130
Hanlet Escaño Avatar answered Oct 12 '22 07:10

Hanlet Escaño


Instead you can hide those columns while exporting to excel using the below code

this.myGridview.Columns[0].Visible = false;

where '0' is the index of the column we want to hide.

like image 25
user2508258 Avatar answered Oct 12 '22 07:10

user2508258