実装する必要がある機能が 1 つあります。
グリッド データを 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
、グリッドビューが Excel にエクスポートされる前に (RowDataBound または同様のイベントで) いくつかの列にあることであり、ファイルをエクスポートしたユーザーはunhide
、Microsoft Excel でファイルを開いた後に非表示の列にアクセスできる必要があります。html
を使用しようとしたときにグリッドビューがレンダリングされるため、これは列を非表示にしていますが、Microsoft Excel ではdisplay:none
できません。unhide
では、Excel にエクスポートする前にグリッド ビューの列を非表示にし、Microsoft Excel でファイルを開くときに列を再表示するにはどうすればよいですか?