在我的Web项目中,我下载了一个xls文件。 我用所需的数据生成一个gridview并下载。 打开excel后,我需要阻止一列。
我的代码如下:
private DataTable tableExcel;
public ActionResult returnExcel(HttpResponseBase response, string name)
{
var grid = new GridView();
grid.DataSource = tableExcel;
grid.DataBind();
GridViewRow HeaderGridRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);
TableCell HeaderCell = new TableCell();
HeaderCell = new TableCell();
HeaderCell.Text = h.text;
HeaderCell.ColumnSpan = colunesSpan;
HeaderCell.Font.Bold = h.negreta;
HeaderCell.HorizontalAlign = h.aliniacio;
HeaderGridRow.Cells.Add(HeaderCell);
HeaderGrid.Controls[0].Controls.AddAt(0, HeaderGridRow);
response.ClearContent();
response.Buffer = true;
response.AddHeader("content-disposition", $"attachment; filename={name}.xls");
response.ContentType = "application/ms-excel";
response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
grid.RenderControl(htw);
response.Output.Write(sw.ToString());
response.Flush();
response.End();
}
代码正确运行,我唯一需要做的就是能够实现阻止或冻结列的选项。
grid.Columns[0].Frozen = true;
grid.Rows[0].CssClass = "locked";
我找到了这些解决方案,但是它们不适用于GridView。