2013-04-09 78 views
0

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

<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> 
+0

请点击这里给我解决我只想电网的数据绑定场争夺w在Excel表格中。 – SANDEEP 2013-04-09 10:15:25

回答

0

做一两件事,当你导出GridView控件retrive从GridView控件(click here)的数据表或主要来源,

创造新的GridView对象中的数据,如

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

使用此GridView控件生成excel表格中

+0

由于Dude..but有我已经结合具有在GETDATA(一个数据表)函数束腰的问题。为此我必须再次调用该过程并填写数据表。 – SANDEEP 2013-04-09 12:39:28

+0

是你可以做的,另一种是得到gridveiw数据表,因为我前面提到的参考这个http://stackoverflow.com/questions/785799/how-can-i-export-a-gridview-datasource-to-a-数据表或 - 数据集 – Gajendra 2013-04-09 12:44:12