Click here to Skip to main content
15,940,921 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I have used the following code for exporting the gridiew data to Excel. But there is a small problem, while exporting the controls are also visible. Can any one please suggest how to disable the controls in the excel sheet after export of data

C#
protected void btnGetData_Click(object sender, EventArgs e)
    {
        try
        {
            GetDataDisplay();
            btnExportExcel.Enabled = true;
        }
        catch(Exception ex)
        {
            throw ex;
        }
        
    }

    protected void GetDataDisplay()
    {
        int sID = Convert.ToInt32(ddlSurvey.SelectedValue.ToString());
        int pcID=0;
        if (ddlSurvey.SelectedItem.Text.ToLower().EndsWith("l1"))
        {
            if (Convert.ToInt32(ddlPC.SelectedIndex) == 0)
            {
                pcID = 0;
                GetSurveyDetails(sID, pcID);
                gvReport.Visible = true;
                tblAvg.Visible = false;
                if (Session["organisationAverage"].ToString() != string.Empty)
                {
                    tblAvg.Visible = true;
                    lblPCAvg.Visible = true;
                    lblOrgAvg.Visible = true;
                    lblorgAvgScore.Visible = true;
                    lblorgAvgScore.Text = Session["organisationAverage"].ToString();
                }
            }
            else
            {
                pcID = Convert.ToInt32(ddlPC.SelectedValue.ToString());
                GetSurveyDetails(sID, pcID);
                gvReport.Visible = true;
                tblAvg.Visible = false;
            }
            ddlPC.Enabled = true;
            
        }
        else
        {
            ddlPC.Enabled = false;
        }
        

    }


  protected void btnExportExcel_Click(object sender, EventArgs e)
    {

        try
        {
            btnExportExcel.Enabled = false;
            ExportToExcel(ExportExcel, ddlPC.SelectedItem.Text.ToString());
            //ExportExcel.Visible = true;
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        return;
    }

    private void ExportToExcel(HtmlGenericControl ExportExcel, string strFileName)
    {
        
            //ExportExcel.Visible = true;
            GetDataDisplay();
            Response.ClearContent();
            Response.AddHeader("content-disposition", "attachment; filename=" + strFileName + ".xls");
            Response.ContentType = "application/excel";
            System.IO.StringWriter sw = new System.IO.StringWriter();
            HtmlTextWriter htw = new HtmlTextWriter(sw);
            ExportExcel.RenderControl(htw);
            Response.Write(sw.ToString());
            Response.End();
    }
Posted
Updated 10-Apr-12 21:23pm
v3
Comments
uspatel 11-Apr-12 3:26am    
what controls you have used?
apr1234 11-Apr-12 5:33am    
button

You can use this custom control to export gridview to excel.
hide that column that have button
add this code on export button click event that you have used
C#
GridView1.DataBind();
if (GridView1.Columns.Count > 0)
    GridView1.Columns[0].Visible = false;//first column hide
else
{
    GridView1.HeaderRow.Cells[0].Visible = false;
    foreach (GridViewRow gvr in GridView1.Rows)
    {
        gvr.Cells[0].Visible = false;
    }
}
 
Share this answer
 
v2
Hi,
For Exporting grid view to excel file use following code

C#
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition","attachment;filename=g.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter ht = new HtmlTextWriter(sw);
gvDisplay.AllowPaging = false;
gvDisplay.DataBind();
gvDisplay.RenderControl(ht);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End()


and just add following metod in aspx.cs page

C#
public override void VerifyRenderingInServerForm(Control control)
    {

    }

Best Luck
happy Coding
 
Share this answer
 
v2
We can add two functions.
one for displaying the controls: this can be used before export excel
and other for hiding the controls: this can be used after export excel
 
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