Click here to Skip to main content
15,885,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a chunk of code to generate excel from Grid View data. But the issue Im facing with this is the code instead of extracting just the grid view data into the excel is copying entire data present in the html UI into the excel file i.e buttons, dorpdown lists e.t.c.

I want to just extract only Grid View data into the excel file. Please help me with this. My code is as below:

protected void btnExcelGenerator_Click(object sender, EventArgs e)
{

    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/vnd.xls";

    System.IO.StringWriter stringWrite = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

    GridViewRefurb.RenderControl(htmlWrite);
    Response.Write(stringWrite.ToString());
    Response.Flush();
    HttpContext.Current.ApplicationInstance.CompleteRequest();

}


Help is deeply appreciated, thanks in advance
Posted

1 solution

Refer this - Remove Edit Delete Button When Exporting GridView To Excel[^].

You just need to hide the appropriate columns before rendering like...
C#
GridView1.Columns[14].Visible = false;
GridView1.Columns[15].Visible = false;
GridView1.Columns[16].Visible = false;
 
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