0

I have GridView and I want to generate excel sheet of the grid column there is code of generate excel and button for click. My problem is that I don't want template field in of grid view column in excel sheet. I want only 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>
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
SANDEEP
  • 1,062
  • 3
  • 14
  • 32

1 Answers1

0

Do one thing when you export gridview retrive the data on datatable from gridview(click here) or main source and

create new gridview object like

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

use this gridview to generating excel sheet

Community
  • 1
  • 1
Gajendra
  • 1,291
  • 1
  • 19
  • 32
  • Thanks Dude..but there is a problem i already bind gird with one datatable in GETDATA() function. for this i have to again call the procedure and fill the data table. – SANDEEP Apr 09 '13 at 12:39
  • yes that you can do and other way is get datatable from gridveiw as i mentioned previously refer this http://stackoverflow.com/questions/785799/how-can-i-export-a-gridview-datasource-to-a-datatable-or-dataset – Gajendra Apr 09 '13 at 12:44