Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Excel DataTable , +
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.
 
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 3-Jan-13 21:54pm
Edited 3-Jan-13 22:39pm
v6
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please find solved example in following link
 
Export Gridview Data to Excel in ASP.NET[^]
 
Thanks
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try This,
 
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();
}
  Permalink  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 535
1 OriginalGriff 275
2 Shweta N Mishra 244
3 Mathew Soji 225
4 BillWoodruff 220
0 OriginalGriff 6,153
1 Sergey Alexandrovich Kryukov 5,758
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,735


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 4 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100