C# 将GridView导出到Excel时,不使用带有红色超链接的GridView单元格

C# 将GridView导出到Excel时,不使用带有红色超链接的GridView单元格,c#,asp.net,excel,gridview,C#,Asp.net,Excel,Gridview,我有一个ASP.NET4.0GridView和一个将GridView数据导出到Excel的按钮。这一切都很好。但是,在GridView中,每行三个单元格中的文本是红色的,因为它们是超链接。当我将数据导出到Excel时,这些单元格仍然是红色的。我希望它们是黑色的。我该怎么做?这是我的密码: Response.ClearContent(); Response.AppendHeader("content-disposition", "attachment; filena

我有一个ASP.NET4.0GridView和一个将GridView数据导出到Excel的按钮。这一切都很好。但是,在GridView中,每行三个单元格中的文本是红色的,因为它们是超链接。当我将数据导出到Excel时,这些单元格仍然是红色的。我希望它们是黑色的。我该怎么做?这是我的密码:

        Response.ClearContent();
        Response.AppendHeader("content-disposition", "attachment; filename=Mobile.xls");
        Response.ContentType = "application/excel";

        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

        foreach (GridViewRow gridViewRow in gvResults.Rows)
        {
            gridViewRow.ForeColor = Color.Black;
            foreach (TableCell gridViewRowTableCell in gridViewRow.Cells)
                gridViewRowTableCell.Style["forecolor"] = "#000000";
            if (gridViewRow.RowType == DataControlRowType.DataRow)
            {
                for (int columnIndex = 0; columnIndex < gridViewRow.Cells.Count; columnIndex++)
                {
                    gridViewRow.Cells[columnIndex].Attributes.Add("class", "text");
                }

            }
        }

        gvResults.RenderControl(htmlTextWriter);
        string style = @"<style> .text { mso-number-format:\@; } </style> ";
        Response.Write(style);
        Response.Write(stringWriter.ToString());
        Response.End();
这是链接的代码。这将运行OnDataBound:

foreach (GridViewRow row in gvResults.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow || row.RowType == DataControlRowType.EmptyDataRow)
            {
                string imei = row.Cells[0].Text;
                if (imei != "&nbsp;")
                {
                    string imeiLink = "window.location='SmartphoneInventory.aspx?imei=" + imei + "';";
                    row.Cells[0].Attributes.Add("onClick", String.Format(imeiLink));
                    row.Cells[0].Text = "<span style='color:red'>" + row.Cells[0].Text + "</span>";
                }

                string phonenumber = row.Cells[1].Text;
                if (phonenumber != "&nbsp;")
                {
                    string phonenumberLink = "window.location='SmartphoneInventory.aspx?phonenumber=" + phonenumber + "';";
                    row.Cells[1].Attributes.Add("onClick", String.Format(phonenumberLink));
                    row.Cells[1].Text = "<span style='color:red'>" + row.Cells[1].Text + "</span>";
                }

                int cellCount = row.Cells.Count;
                string empName = row.Cells[cellCount - 1].Text;
                if (empName != "&nbsp;")
                {
                    string empNameLink = "window.location='DevicesByEmployee.aspx?empName=" + empName + "';";
                    row.Cells[cellCount - 1].Attributes.Add("onClick", String.Format(empNameLink));
                    row.Cells[cellCount - 1].Text = "<span style='color:red'>" + row.Cells[cellCount - 1].Text + "</span>";
                }
            }
        }     

我想我需要的是如何重新设计跨度。这可能吗?GridView自动生成其列。

获取链接按钮文本,将其分配给GridView单元格文本并删除控件

下面是我在标题中所做的:

C

VB.NET

编辑:

创建扩展方法:

C

VB.NET

它将从字符串中删除标记,从而删除样式。使用方法如下:

    foreach (GridViewRow gridViewRow in gvResults.Rows)
    {
        gridViewRow.ForeColor = Color.Black;
        foreach (TableCell gridViewRowTableCell in gridViewRow.Cells)
            gridViewRowTableCell.Style["forecolor"] = "#000000";
        if (gridViewRow.RowType == DataControlRowType.DataRow)
        {
            for (int columnIndex = 0; columnIndex < gridViewRow.Cells.Count; columnIndex++)
            {
                gridViewRow.Cells[columnIndex].Text = gridViewRow.Cells[columnIndex].Text.StripTags();
            }

        }
    }

尼基塔-谢谢,但我在GridView中没有链接按钮。请参阅我上面问题中添加的代码,了解我是如何创建链接的。Nikita-这非常有效,谢谢!为了供其他读者参考,我创建了一个名为ExtensionMethods的公共静态类,我在其中放置了StripTags函数。该类不在名称空间中,因为我的项目中有各种名称空间。我还发现我需要将参数作为striptags这个字符串html传递,并添加关键字this。再次感谢你!一、 就个人而言,爱扩展方法是多么有用!如果您添加ref,它会变得更酷,代码更少:玩得开心!此解决方案不删除链接,而是删除所有文本。此外,一些单元是空的,并且该解决方案不考虑这一点。
For Each c As TableCell In gv.HeaderRow.Cells
    If c.HasControls Then
        c.Text = TryCast(c.Controls(0), LinkButton).Text
        c.Controls.Clear()
    End If
Next
[Extension()]
public string StripTags(string html)
{
    return Regex.Replace(html, "<.*?>", "");
}
<Extension> _
Public Function StripTags(html As String) As String
    Return Regex.Replace(html, "<.*?>", "")
End Function
    foreach (GridViewRow gridViewRow in gvResults.Rows)
    {
        gridViewRow.ForeColor = Color.Black;
        foreach (TableCell gridViewRowTableCell in gridViewRow.Cells)
            gridViewRowTableCell.Style["forecolor"] = "#000000";
        if (gridViewRow.RowType == DataControlRowType.DataRow)
        {
            for (int columnIndex = 0; columnIndex < gridViewRow.Cells.Count; columnIndex++)
            {
                gridViewRow.Cells[columnIndex].Text = gridViewRow.Cells[columnIndex].Text.StripTags();
            }

        }
    }