我有一个 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 自动生成其列。