[英]Exporting GridView to Excel without gridview cells with hyperlinks showing red
我有一个ASP.Net 4.0 GridView和一个将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 != " ")
{
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 != " ")
{
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 != " ")
{
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自动生成其列。
获取LinkButton文本,将其分配给GridView单元文本并删除控件。
这是我在标题中执行的操作:
C#
foreach (TableCell c in gv.HeaderRow.Cells) {
if (c.HasControls) {
c.Text = (c.Controls(0) as LinkButton).Text;
c.Controls.Clear();
}
}
VB.NET
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
编辑:
创建扩展方法:
C#
[Extension()]
public string StripTags(string html)
{
return Regex.Replace(html, "<.*?>", "");
}
VB.NET
<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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.