我有一個需要實現的功能。如何使用asp.net隱藏和取消隱藏excel工作表的列
我們將網格數據綁定到excel導出,並且工作正常。 但我得到了新的要求,我必須隱藏Excel導出中的列。 之後,當用戶打開excel工作表時,他應該可以選擇隱藏通過代碼隱藏的列。
編輯:
我有我的.aspx頁面上的GridView控件其中我出口使用下面的代碼,以擅長:
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(@"<style> .sborder { color : Black;border : 1px Solid Black; } .yellow {background-color:yellow;color:black;} </style> ");
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
}
的要求是hide
一些列(在的RowDataBound或類似事件),然後將gridview導出爲excel,導出該文件的用戶應該能夠在Microsoft Excel中打開該文件後隱藏列unhide
。由於網格視圖被重新編號爲html
我曾嘗試使用display:none
,這是隱藏列,但我不能在它在Microsoft Excel中。
那麼如何隱藏網格視圖列,然後將其導出到excel並在我在Microsoft Excel中打開文件時取消隱藏列?
@JeremyThompson他希望它在ASP.NET代碼中 – logixologist