Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to hide and unhide the columns of an excel sheet using asp.net
    text
    copied!<p>I am having one functionality which need to be implemented.</p> <p>We are binding the Grid data to the excel export and it is working fine. But i got the new requirement where i have to hide the columns in the excel export. After that when user open the excel sheet he should have the option to UN-hide again the columns which we hide through code.</p> <p>Edit: </p> <p>I have a gridview control on my .aspx page which I am exporting to excel using the following code:</p> <pre><code> public static void Export(string filename, GridView grid) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", filename.Replace(" ", "") + ".xls")); HttpContext.Current.Response.Charset = ""; HttpContext.Current.Response.ContentType = "application/vnd.xls"; StringWriter sw = new StringWriter(); HtmlTextWriter htw = new HtmlTextWriter(sw); grid.HeaderStyle.BackColor = System.Drawing.Color.Cyan; GridViewRow row = new GridViewRow(0, 1, DataControlRowType.DataRow, DataControlRowState.Normal); TableCell cell = new TableCell(); cell.Text = String.Format("{0}", Heading[count]); cell.ColumnSpan = grid.Rows[1].Cells.Count; cell.Attributes.Add("style", "background-color: white; color: black;text-align:left;"); cell.Attributes.Add("class", "yellow"); row.Cells.Add(cell); grid.Controls[0].Controls.AddAt(0, row); grid.RenderControl(htw); DataTable dt = new DataTable(); DataRow dr; dt.Columns.Add(new System.Data.DataColumn(" ", typeof(String))); dr = dt.NewRow(); dr[0] = " "; dt.Rows.Add(dr); GridView gvSpace = new GridView(); gvSpace.DataSource = dt; gvSpace.GridLines = 0; gvSpace.DataBind(); gvSpace.RenderControl(htw); grid.HeaderStyle.BackColor = System.Drawing.Color.Cyan; HttpContext.Current.Response.Write(@"&lt;style&gt; .sborder { color : Black;border : 1px Solid Black; } .yellow {background-color:yellow;color:black;} &lt;/style&gt; "); HttpContext.Current.Response.Write(sw.ToString()); HttpContext.Current.Response.End(); HttpContext.Current.Response.Flush(); } </code></pre> <p>The requirement is to <code>hide</code> some columns (in RowDataBound or a similar event) before the gridview is exported to excel and the user who exported the file should be able to <code>unhide</code> the hidden columns after opening the file in Microsoft Excel. As the grid view is rendenred as <code>html</code> i have tried using <code>display:none</code>, this is hiding the column but I am not able to <code>unhide</code> it in the Microsoft excel.</p> <p>So how can I hide grid view columns before exporting it to excel and unhide the columns when I open the file in Microsoft excel? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload