Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gridview to Excel

I have a gridview with AutoGenerateDeleteButton

enter image description here

And i export this gridview simple this code..

Response.Clear();
        Response.AddHeader("content-disposition", "attachment;filename=Avukat.xls");
        Response.Charset = "";

        Response.ContentType = "application/vnd.xls";
        System.IO.StringWriter stringWrite = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
        GridView1.RenderControl(htmlWrite);
        Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
        Response.Write(stringWrite.ToString());
        Response.End();

There is no problem with that.

But in the excel there is a delete column :))

enter image description here

How can i delete the delete column in excel?

like image 877
Soner Gönül Avatar asked Feb 21 '11 22:02

Soner Gönül


People also ask

How do I export data from ASP net to excel?

Add a link button in the View and name it DownloadExcel. Now, create a Method in Controller and add the following code to Export the data in Excel. Now, run the project and click on the "Download Excel" button. The data is downloaded in Excel format.


2 Answers

Your problem is you want 2 x "views" of your grid. With & without the delete column.

One for the browser, and one for Excel.

On your export, drop the delete column. Something something like:

    GridView1.AutoGenerateDeleteButton = false;
    GridView1.DataBind(); 
    GridView1.RenderControl(htmlWrite);
    Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
like image 59
Christian Payne Avatar answered Sep 20 '22 01:09

Christian Payne


You need to remove the column from the GridView before exporting it.

like image 37
SLaks Avatar answered Sep 22 '22 01:09

SLaks