I have tried code for saving entire gridview data to an excel file and it is working fine but now i want to save only particular columns of gridview to an excel file.I have used mysql database and asp.net with C#.Please anyone help me with this.
**default.aspx**
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:Button ID="btnSave" runat="server" Text="Save to Excel" OnClick="btnSave_Click" />
**default.cs**
protected void btnSave_Click(object sender, EventArgs e)
{
ExportGridToExcel(GridView1, "StudentMarks.xls");
}
public void ExportGridToExcel(GridView grdGridView, string fileName)
{
Response.Clear();
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", fileName));
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
StringWriter stringWrite = new StringWriter();
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
grdGridView.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
return;
}