Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 导出到Excel时,如何在GridView中使用“显示名称”属性?_C#_Asp.net Mvc_Gridview - Fatal编程技术网

C# 导出到Excel时,如何在GridView中使用“显示名称”属性?

C# 导出到Excel时,如何在GridView中使用“显示名称”属性?,c#,asp.net-mvc,gridview,C#,Asp.net Mvc,Gridview,我想在将列表导出到Excel文件时使用[DisplayName()]属性设置列标题文本。 有解决办法吗 GridView gv = new GridView(); gv.DataSource = ExportList; gv.DataBind(); System.Web.HttpContext.Current.Response.ClearContent(); System.Web.HttpContext.Current.Response.Buffer = true; System.We

我想在将列表导出到Excel文件时使用[DisplayName()]属性设置列标题文本。 有解决办法吗

GridView gv = new GridView();
gv.DataSource = ExportList;

gv.DataBind();

 System.Web.HttpContext.Current.Response.ClearContent();
 System.Web.HttpContext.Current.Response.Buffer = true;
 System.Web.HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=Rapor.xls");
 System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
 System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
 System.Web.HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
System.Web.HttpContext.Current.Response.Output.Write(sw.ToString());
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();

您必须像这样使用网格的
RowDataBound
事件: 创建gv后,首先放置此行:

gv.RowDataBound+=GvOnRowDataBound;
然后按照以下代码操作:

private void GvOnRowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        for (int i = 0; i < e.Row.Cells.Count;i++)
        {
            var customAttributes =
                typeof (YourClass).GetProperty(e.Row.Cells[i].Text).CustomAttributes.ToList();
            var displayNameAttribute =
                customAttributes.FirstOrDefault(
                    aa => aa.AttributeType.FullName.Equals("System.ComponentModel.DisplayNameAttribute"));
            if (displayNameAttribute != null)
                e.Row.Cells[i].Text = displayNameAttribute.ConstructorArguments[0].ToString();
        }
    }
}
private void GvOnRowDataBound(对象发送方,GridViewRowEventArgs e)
{
if(e.Row.RowType==DataControlRowType.Header)
{
for(int i=0;iaa.AttributeType.FullName.Equals(“System.ComponentModel.DisplayNameAttribute”);
if(displayNameAttribute!=null)
e、 Row.Cells[i].Text=displayNameAttribute.ConstructorArguments[0].ToString();
}
}
}

请记住将
YourClass
更改为您的班级名称。

谢谢。。。这节省了我很多时间。只有几个观察结果可以帮助其他人:1-在调用DataBind()方法之前调用它。2-要从标题中删除(“),请将代码中的行替换为->e.Row.Cells[i]。Text=displayNameAttribute.ConstructorArguments[0]。Value.ToString();