私は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>