Click here to Skip to main content
15,907,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my page I have two textbox controls, where I am gating the date from the Calendar Extender & in the export to excel Button I am going to export the Gridview data to excel sheet. When I am gating the excel sheet its show the the textbox & button also from which i am export the excel Sheet.

I have written the export code in the Export Button. Like:-

C#
protected void Export_to_Excel_Click(object sender, EventArgs e)
    {
        try
        {

            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
            Response.ContentType = "application/ms-excel";
            StringWriter sw = new StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            Grd_MidData.AllowPaging = false;
            bindgriddata();
            //Change the Header Row back to white color
            Grd_MidData.HeaderRow.Style.Add("background-color", "#FFFFFF");
            //Applying stlye to gridview header cells
            for (int i = 0; i < Grd_MidData.HeaderRow.Cells.Count; i++)
            {
                Grd_MidData.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");
            }
            Grd_MidData.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.Flush();
        }
        catch (Exception ee)
        {

        }
    }


When I am gating the excel sheet it shows the gridview data along with the two text box & the button from where I am doing the filtering.

So any one suggest me, How to show only the gridview data but not show the Textbox & button on the excel sheet.

What I have tried:

Here the gridview data is showing along with the From date: To Date: textbox & the button also. So how can i remove these field from the Excel sheet.
Posted
Updated 9-May-16 1:55am
v2
Comments
Karthik_Mahalingam 9-May-16 7:38am    
post the markup code..

1 solution

try this

C#
protected void Export_to_Excel_Click(object sender, EventArgs e)
        {

            try
            {
                GridView gv = new GridView();
                Grd_MidData.AllowPaging = false;
                BindCustomersDDL(); 
                gv = Grd_MidData;  // cloning the existing grid, so that it will not disturb the existing grid

                Response.ClearContent();
                Response.Buffer = true;
                Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
                Response.ContentType = "application/ms-excel";
                StringWriter sw = new StringWriter();
                HtmlTextWriter htw = new HtmlTextWriter(sw);

                for (int i = 0; i < Grd_MidData.HeaderRow.Cells.Count; i++)
                    gv.HeaderRow.Cells[i].Style.Add("background-color", "#df5015");


                // Hiding the unwanted columns in the excel output 
                int[] columnsToBeHidden = { 2,8 }; // zero based index, this will hide 3rd ad 9th column 
                for (int i = 0; i < columnsToBeHidden.Length; i++)
                    gv.Columns[i].Visible = false;



                gv.RenderControl(htw);
                Response.Write(sw.ToString());
                Response.Flush();
            }
            catch (Exception ee)
            {

            }
        }


Pass the index of the column which you want to hide
C#
int[] columnsToBeHidden = { 5,6};
 
Share this answer
 
v2

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