在生成excel表格时不需要网格视图模板字段

本文关键字:视图 网格 字段 不需要 excel 表格 | 更新日期: 2023-09-27 18:08:26

我有GridView,我想生成网格列的excel表有代码生成excel和按钮点击。我的问题是我不希望模板字段在网格视图列在excel表格。我只想要BoundField DataField

<asp:ImageButton ID="btnExcel" runat="server" Text="Excel" ToolTip="Excel"  ImageUrl="~/Images/Resources/thumb/excel.png" OnClick="btnExcel_Click" />
protected void btnExcel_Click(object sender, ImageClickEventArgs e)
{
    ExportToExcel("Repoet.xls", grdReq);
}

public void ExportToExcel(string strFileName, GridView gv)
{
        HtmlForm form = new HtmlForm();
        Response.ClearContent();
        Response.Buffer = true;
        Response.AddHeader("content-disposition", "attachment; filename=" +      strFileName);
        Response.ContentType = "application/excel";
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        form.Controls.Add(gv);
        this.Controls.Add(form);
        form.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }
}

<asp:GridView ID="grdReq" runat="server" AutoGenerateColumns="false" CssClass="SimpleGrid"
                OnRowDataBound="grdReq_RowDataBound">
<Columns>
 <asp:BoundField DataField="CandidateID" HeaderText="Candidate Id" />
 <asp:TemplateField>
 <ItemTemplate>
   <asp:HyperLink ID="hlnkEvalSheet" runat="server" Text='<%#Eval("Candidate Name") %>'
    CssClass="logo" NavigateUrl='<%# "~/Recruiter/EvalSheets.aspx?ClientId=" +      hdnClientId.Value  + "&ReqId=" + hdnReqId.Value  %>'
    ImageUrl="~/Images/Resources/search.png" >
    </asp:HyperLink>
 </ItemTemplate>
 </asp:TemplateField>
<asp:BoundField DataField="Candidate Name" HeaderText="Candidate Name" />
<asp:BoundField DataField="Organization" HeaderText="Organization" />                     <asp:BoundField DataField="Desig" HeaderText="Design" /> 
<asp:BoundField DataField="Overall" HeaderText="Overall" />
<asp:BoundField DataField="Qualification" HeaderText="Qualification" /> 
<asp:BoundField DataField="Location" HeaderText="Location" />
<asp:BoundField DataField="Current CTC (LPA)" HeaderText="Current CTC (LPA)" />
</Columns>
</asp:GridView>

在生成excel表格时不需要网格视图模板字段

导出gridview时做一件事从gridview(点击这里)或主源中检索数据表上的数据

创建新的gridview对象

  Gridview gvexport=new Gridview();
  gvexport.Datasource=dt //retrived data
  gvexport.databind();

使用gridview生成excel表格