Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
i want to download data of datatable in excel. in VS 2005
i tried many method but its no use now i am using following approach.

C#
public void ExportToExcel(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                Response.Clear();
                string filename = "Breakup wise Stock Summary.xls";
                System.IO.StringWriter tw = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
                DataGrid dgGrid = new DataGrid();
                dgGrid.DataSource = dt;
                dgGrid.DataBind();

                
                dgGrid.RenderControl(hw);
                //Response.ContentType = application/vnd.ms-excel;
                Response.ContentType = "application/vnd.ms-excel";
                //Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
                this.EnableViewState = false;
                Response.Write(tw.ToString());
                Response.End();
            }
        }

but getting error.
sys.webforms.PageRequestManagerParsererrorException: The message recieved from the server could not be parsed. Common cause for this error are when the responce is modified by calls to Response.Write(),response filter,HttpModules, or server trace is enable.
Detail:Error parsing near
Posted
Updated 3-Jan-13 21:39pm
v6

Please find solved example in following link

Export Gridview Data to Excel in ASP.NET[^]

Thanks
 
Share this answer
 
Try This,

C#
public void ExportToExcel()
{
this.EnableViewState = false;
Response.Charset = string.Empty;
BindDataGrid(false); //Function for Binding Grid,false indicates allopaging is false
   Response.AddHeader("content-disposition",   "attachment;filename=CategoryList_" + System.DateTime.Now.ToShortDateString() + ".xls");
            Response.ContentType = "application/ms-excel";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            System.Web.UI.HtmlControls.HtmlForm htmlform = new System.Web.UI.HtmlControls.HtmlForm();
            htmlform.Attributes["runat"] = "Server";
            dgvCategory.Parent.Controls.Add(htmlform);
            htmlform.Controls.Add(dgvCategory);
            htmlform.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            //gvEmployeePrint.Visible = false;
            Response.End();
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900