title: Exporting GridView to Excel without gridview cells with hyperlinks showing red
body: <p>I have an ASP.Net 4.0 GridView and a button which exports the GridView data to Excel. This all works fine. However, within the GridView, the text in three cells of each row are red because they are hyperlinks. When I export the data to Excel, these cells are still red. I want them to be black. How do I do this?  Here's my code:</p>
  
  <pre><code>        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 &lt; gridViewRow.Cells.Count; columnIndex++)
                  {
                      gridViewRow.Cells[columnIndex].Attributes.Add("class", "text");
                  }
  
              }
          }
  
          gvResults.RenderControl(htmlTextWriter);
          string style = @"&lt;style&gt; .text { mso-number-format:\@; } &lt;/style&gt; ";
          Response.Write(style);
          Response.Write(stringWriter.ToString());
          Response.End();
  </code></pre>
  
  <p>And here's the code for the links. This runs OnDataBound:</p>
  
  <pre><code>foreach (GridViewRow row in gvResults.Rows)
          {
              if (row.RowType == DataControlRowType.DataRow || row.RowType == DataControlRowType.EmptyDataRow)
              {
                  string imei = row.Cells[0].Text;
                  if (imei != "&amp;nbsp;")
                  {
                      string imeiLink = "window.location='SmartphoneInventory.aspx?imei=" + imei + "';";
                      row.Cells[0].Attributes.Add("onClick", String.Format(imeiLink));
                      row.Cells[0].Text = "&lt;span style='color:red'&gt;" + row.Cells[0].Text + "&lt;/span&gt;";
                  }
  
                  string phonenumber = row.Cells[1].Text;
                  if (phonenumber != "&amp;nbsp;")
                  {
                      string phonenumberLink = "window.location='SmartphoneInventory.aspx?phonenumber=" + phonenumber + "';";
                      row.Cells[1].Attributes.Add("onClick", String.Format(phonenumberLink));
                      row.Cells[1].Text = "&lt;span style='color:red'&gt;" + row.Cells[1].Text + "&lt;/span&gt;";
                  }
  
                  int cellCount = row.Cells.Count;
                  string empName = row.Cells[cellCount - 1].Text;
                  if (empName != "&amp;nbsp;")
                  {
                      string empNameLink = "window.location='DevicesByEmployee.aspx?empName=" + empName + "';";
                      row.Cells[cellCount - 1].Attributes.Add("onClick", String.Format(empNameLink));
                      row.Cells[cellCount - 1].Text = "&lt;span style='color:red'&gt;" + row.Cells[cellCount - 1].Text + "&lt;/span&gt;";
                  }
              }
          }     
  </code></pre>
  
  <p>I guess what I need is how to re-style the span. Is this possible? The GridView autogenerates its columns.</p>