To view or save the contents of a gridview in an excel document, we will use a button click event which will implement export.
Although our gridview has paging export all registros.
protected void Button1_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter htw = new HtmlTextWriter(sw);
Page page = new Page();
HtmlForm form = new HtmlForm();
GridView1.EnableViewState = false;
GridView1.AllowPaging = false;
GridView1.DataBind();
page.EnableEventValidation = false;
page.DesignerInitialize();
page.Controls.Add(form);
form.Controls.Add(GridView1);
page.RenderControl(htw);
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application / ms-excel";
Response.AddHeader ("Content-Disposition", "attachment; filename = nombreDocumento.xls");
Response.Charset = "UTF-8 "
Response.Cache.SetCacheability (HttpCacheability.NoCache)
Response.ContentEncoding = System.Text.Encoding.Default;
Response.Write (sb.ToString ());
Response.End ();}
0 comments:
Post a Comment