Ellen Sellers
Ellen Sellers

Reputation: 191

How to hide to columns when exporting GridView to Excel?

How can I hide two columns when I export my GridView to excel.

So at the moment this is how it looks like when I export:

-------------------------------------------------------------------------
|  Display Name  | Email Address    | License  |            |           |
-------------------------------------------------------------------------
| User 1         | [email protected]  |   1      |   edit     |  delete   |
-------------------------------------------------------------------------
| User 2         | [email protected] |   1      |   edit     |  delete   |
-------------------------------------------------------------------------

As you can see the last two columns are links within my GridView to edit and delete the row Those two columns I don't want to export to excel as it looks unprofessional.

Is there a way to remove those two columns (or hide) when exporting to excel?

This is my code I am using:

protected void ExportToExcel(object sender, EventArgs e)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment;filename=UserExport.xls");
        Response.Charset = "";
        Response.ContentType = "application/vnd.ms-excel";
        using (StringWriter sw = new StringWriter())
        {
            HtmlTextWriter hw = new HtmlTextWriter(sw);

            //To Export all pages
            GridView1.AllowPaging = false;
            this.BindGrid();

            GridView1.HeaderRow.BackColor = Color.White;
            foreach (TableCell cell in GridView1.HeaderRow.Cells)
            {
                cell.BackColor = GridView1.HeaderStyle.BackColor;
            }
            foreach (GridViewRow row in GridView1.Rows)
            {
                row.BackColor = Color.White;
                foreach (TableCell cell in row.Cells)
                {
                    if (row.RowIndex % 2 == 0)
                    {
                        cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
                    }
                    else
                    {
                        cell.BackColor = GridView1.RowStyle.BackColor;
                    }
                    cell.CssClass = "textmode";
                }
            }

            GridView1.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();
        }
    }

Upvotes: 2

Views: 1435

Answers (2)

Random Stuff
Random Stuff

Reputation: 170

This might work. It should basically copy the gridview to a new gridveiw and then remove the last two columns ready for the rest of your code.

protected void ExportToPDF(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=UserExport.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    using (StringWriter sw = new StringWriter())
    {
        HtmlTextWriter hw = new HtmlTextWriter(sw);


GridView gv = GridView1;
gv.Columns.RemoveAt(4);
gv.Columns.RemoveAt(3);

        //To Export all pages
        gv.AllowPaging = false;
        this.BindGrid();

        gv.HeaderRow.BackColor = Color.White;
        foreach (TableCell cell in gv.HeaderRow.Cells)
        {
            cell.BackColor = gv.HeaderStyle.BackColor;
        }
        foreach (GridViewRow row in gv.Rows)
        {
            row.BackColor = Color.White;
            foreach (TableCell cell in row.Cells)
            {
                if (row.RowIndex % 2 == 0)
                {
                    cell.BackColor = gv.AlternatingRowStyle.BackColor;
                }
                else
                {
                    cell.BackColor = gv.RowStyle.BackColor;
                }
                cell.CssClass = "textmode";
            }
        }

        gv.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();
    }
}

Upvotes: 3

Foitn
Foitn

Reputation: 746

Well a not so elegant solution can always be that you use just a normal for loop instead of the foreach. This would mean that you can just skip the last two columns.

protected void ExportToExcel(object sender, EventArgs e)
{
    Response.Clear();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment;filename=UserExport.xls");
    Response.Charset = "";
    Response.ContentType = "application/vnd.ms-excel";
    TableCell cell;
    using (StringWriter sw = new StringWriter())
    {
        HtmlTextWriter hw = new HtmlTextWriter(sw);

        //To Export all pages
        GridView1.AllowPaging = false;
        this.BindGrid();

        GridView1.HeaderRow.BackColor = Color.White;
        foreach (TableCell cell in GridView1.HeaderRow.Cells)
        {
            cell.BackColor = GridView1.HeaderStyle.BackColor;
        }
        foreach (GridViewRow row in GridView1.Rows)
        {
            row.BackColor = Color.White;
            for(int i = 0; i < row.Cells.Count()-2; i++)
            {
                cell = row.Cells[i];
                if (row.RowIndex % 2 == 0)
                {
                    cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
                }
                else
                {
                    cell.BackColor = GridView1.RowStyle.BackColor;
                }
                cell.CssClass = "textmode";
            }
        }

        GridView1.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();
    }
}

Upvotes: 0

Related Questions