日期:2014-05-17  浏览次数:20550 次

在gridview导出为excel时,如何不导出HyperLinkField?
在gridview导出为excel时,如何不导出HyperLinkField?

gridview中有一个HyperLinkField,如何在导出excel时,不导出,
主要导出方法:
  StringWriter sw = new StringWriter();
  HtmlTextWriter hw = new HtmlTextWriter(sw);
  GridView1.RenderControl(hw);
  Response.Write(sw.ToString());
  Response.End();

------解决方案--------------------
导出的datagridview可以和显示的不同,用另一个,当导出时绑定上数据,并且不放HyperLinkField,只放数据。
弹出窗口给HyperLinkField加个属性,用onclick,用js实现.

------解决方案--------------------
例子
HTML code
<%@ Page Language="C#" Debug="true" %>

<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  public bool isExport = false;

  private System.Data.DataTable GetData()
  {
    System.Data.DataTable dt = new System.Data.DataTable();
    System.Data.DataRow dr;
    dt.Columns.Add(new System.Data.DataColumn("学生班级", typeof(System.String)));
    dt.Columns.Add(new System.Data.DataColumn("学生姓名", typeof(System.String)));
    dt.Columns.Add(new System.Data.DataColumn("语文", typeof(System.Decimal)));
    System.Random rd = new System.Random();
    for (int i = 0; i < 8; i++)
    {
      dr = dt.NewRow();
      dr[0] = "班级" + i.ToString();
      dr[1] = "【孟子E章】" + i.ToString();
      dr[2] = System.Math.Round(rd.NextDouble() * 100, 0);
      dt.Rows.Add(dr);
    }
    return dt;
  }
  protected void Page_Load(object sender, EventArgs e)
  {
    
    if (!Page.IsPostBack)
    {

      GridView1.DataSource = GetData();
      GridView1.DataBind();
    }
  }

  protected void Button1_Click(object sender, EventArgs e)
  {
    isExport = true;   
    Response.Clear();
    Response.Buffer = true;
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
    Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
    Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
    System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
    System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
    GridView1.DataSource = GetData();
    GridView1.DataBind();
    this.GridView1.RenderControl(oHtmlTextWriter);
    Response.Output.Write(oStringWriter.ToString());
    Response.Flush();
    Response.End();
  }
  public override void VerifyRenderingInServerForm(Control control) { }
  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
    if (isExport)
    {
      e.Row.Cells[1].Visible = false;
    }
    else
    {
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
        HyperLink x = e.Row.Cells[1].Controls[0] as HyperLink;
        x.Attributes.Add("onclick", "window.open('','_xx','width=200,height=300,left=200');");
        x.Target = "_xx";
      }
    }
  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
</head>
<body>
  <form id="form1" runat="server">
  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
    <Columns>
      <asp:BoundField DataField="学生班级" />
      <asp:HyperLinkField DataNavigateUrlFields="学生班级" DataTextField="学生姓名" DataNavigateUrlFormatString="xx.aspx?id={0}" />
    </Col