Gridview in excel

I have a gridview with AutoGenerateDeleteButton

enter image description here

And I export this gridview just 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(); 

No problem with that.

But excel has a delete column :))

enter image description here

How to delete delete column in excel?

+7
source share
3 answers

Your problem is that you want 2 x โ€œviewsโ€ of your grid. With and without a delete column.

One for the browser and one for Excel.

In your export, undo the delete column. Something like:

  GridView1.AutoGenerateDeleteButton = false; GridView1.DataBind(); GridView1.RenderControl(htmlWrite); Response.Write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />"); 
+10
source

Before exporting, you need to remove the column from the GridView .

+4
source

... Or you can export to a real Excel 2007 .xlsx file properly using OpenXML libraries.

Using this C # library, you just need to include the CreateExcelFile class in your application and call one function:

 DataSet ds = CreateSampleData(); CreateExcelFile.CreateExcelDocument(ds, "C:\\Sample.xlsx"); 

Full source code and demo application are provided free of charge.
http://www.mikesknowledgebase.com/pages/CSharp/ExportToExcel.htm

Good luck

+2
source

All Articles