8

実装する必要がある機能が 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 でファイルを開くときに列を再表示するにはどうすればよいですか?

4

3 に答える 3

3

複数の列を非表示にする場合:

var targetColumns = 5; // Some value for the cumber of columns you want to hide
for (int i = 1; i < targetColumns ; i++)
{
    yourWorksheet.Column(i).Hidden = true;
}
于 2016-01-15T20:23:47.030 に答える
3

これを使って。

行用

worksheet_sub.Row(i).Height = 0;

カラム用

worksheet_sub.Column(i).Width= 0;

i列または行のインデックスの数は次のとおりです

于 2015-12-16T11:41:01.990 に答える