2013-04-09 63 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

相關問題