Link to home
Start Free TrialLog in
Avatar of fantasylan
fantasylan

asked on

How to save the contents of a GridView to an excel spreadsheet

I am trying to save my GridView data as an Excel file.


Here's my code



 Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=Report.xls");
           // Response.ContentType = "application/octet-stream";

            // If you want the option to open the Excel file without saving then
            // comment out the line below
            //Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.xls";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
           
            GridView3.RenderControl(htmlWrite);
         
            Response.Write(stringWrite.ToString());
            Response.End();

It doesn't work and I get a I get an error about Managed Pipelines. After changing it to Integrated, I still cant get it to work.

Regards.
ASKER CERTIFIED SOLUTION
Avatar of penndell
penndell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial