protected void btnExportToExcel_Click(object sender, EventArgs e) { if (GridView1 == null || GridView1.Rows.Count == 0) { return; } List <int> mylist = hideCols(false); mylist.Reverse(); if (table.IsReadOnly) { GridView1.HeaderRow.Cells[0].Visible = false; } foreach (int i in mylist) { GridView1.HeaderRow.Cells[i].Visible = false; } //WITHOUT THIS WILL WRITE HTML TO EXCEL - MUST SET IT TO FALSE AND AT THE END TO TRUE //NOTE: BY THIS WE DECIDE IF TO SET IMAGES OT NOT -> NO WE DONT WANT IMAGES AT EXCEL GridView1.EnableSortingAndPagingCallbacks = false; Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=" + table.Name + "_" + DateTime.UtcNow.ToString("yyyy_mm_dd_HH_mm_ss") + ".xls"); Response.ContentType = "application/vnd.ms-excel;"; //Response.ContentType = "text/csv"; Response.ContentEncoding = System.Text.Encoding.Unicode; Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble()); //SHURON REAL REMOVED removeHeader(); using (StringWriter sw = new StringWriter()) { HtmlTextWriter hw = new HtmlTextWriter(sw); GridView1.RenderBeginTag(hw); GridView1.HeaderRow.RenderControl(hw); //if (table.IsReadOnly) GridView1.HeaderRow.Cells[0].Visible = false; int g = 0; for (int t = 0; t <= GridView1.PageCount - 1; t++) { List <int> invisible = new List <int>(); GridView1.PageIndex = t; GridView1.DataBind(); //if (t > 20 || g > 20000) continue; foreach (GridViewRow row in GridView1.Rows) { //row.Cells[0].Visible = false; //HIDE ROW DATA CELLS WE DONT WANT TO EXORRT foreach (int i in mylist) { row.Cells[i].Visible = false; } if (table.IsReadOnly) { row.Cells[0].Visible = false; } //if (table.IsReadOnly) row.Cells[0].Visible = false; //HIDE FROM PK CELL ALL DATA UNRELATED TO PRIMARY KEY for (int i = 0; i <= row.Cells[0].Controls.Count - 1; i++) { if (i != 1) { row.Cells[0].Controls[i].Visible = false; } } row.RenderControl(hw); } } GridView1.FooterRow.RenderControl(hw); GridView1.RenderEndTag(hw); string style = @"<style> .textmode { } </style>"; Response.Write(style); Response.Output.Write(sw.ToString()); Response.Flush(); Response.End(); } GridView1.EnableSortingAndPagingCallbacks = true; hideCols(true); }