Export Gridview data to Excel
i) Get the Data from the Database and Bind it to the Gridview
ii) Set Gridview Allowpaging property to "False"
Write the below code to export the entire Gridview data to Excel sheet
private void ExportGridView()
{
string style = @"<style> .text { mso-number-format:\@; } </style> ";
string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
VerificationTrackingGridView.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(VerificationTrackingGridView);
frm.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
}
ii) Set Gridview Allowpaging property to "False"
Write the below code to export the entire Gridview data to Excel sheet
private void ExportGridView()
{
string style = @"<style> .text { mso-number-format:\@; } </style> ";
string attachment = "attachment; filename=Contacts.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
VerificationTrackingGridView.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(VerificationTrackingGridView);
frm.RenderControl(htw);
Response.Write(style);
Response.Write(sw.ToString());
Response.End();
}
Share this post
To be informed of the latest articles, subscribe:
Comment on this post
B
M
M
F
S