【发布时间】:2013-07-03 05:51:53
【问题描述】:
我有一个需要实现的功能。
我们正在将 Grid 数据绑定到 excel 导出,它工作正常。 但是我得到了新的要求,我必须隐藏 excel 导出中的列。 之后,当用户打开 Excel 表时,他应该可以选择再次取消隐藏我们通过代码隐藏的列。
编辑:
我的 .aspx 页面上有一个 gridview 控件,我使用以下代码将其导出到 excel:
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();
}
要求是在gridview导出到excel之前hide一些列(在RowDataBound或类似事件中)并且导出文件的用户应该能够在Microsoft中打开文件后unhide隐藏列Excel。由于网格视图被渲染为html,我尝试使用display:none,这隐藏了该列,但我无法在Microsoft excel中unhide它。
那么,如何在将网格视图列导出到 excel 之前隐藏网格视图列,并在 Microsoft excel 中打开文件时取消隐藏列?
【问题讨论】:
-
@JeremyThompson 他希望在 ASP.NET 代码中使用它