Avatar of searchsanjaysharma
searchsanjaysharma
 asked on

This code of export to excel saves the whole page with extension .aspx

How to correct this .xls or .xlsx

  private void ExportToExcel(string strFileName, GridView dg)
    {
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/vnd.ms-excel";
        Response.Charset = "";
        this.EnableViewState = false;
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        GridView1.RenderControl(oHtmlTextWriter);
        Response.Write(oStringWriter.ToString());
        Response.End();
    }
C#ASP.NET

Avatar of undefined
Last Comment
searchsanjaysharma

8/22/2022 - Mon
AndyAinscow

>>This code of export to excel saves the whole page with extension .aspx
>>How to correct this .xls or .xlsx

Hmmmmm.  Telling us what the problem is would help.
AndyAinscow

ps.  If you wanted an xls file then is writing to a HTMLTextWriter a good idea?
searchsanjaysharma

ASKER
The problem is i want .xls extension, so that whenever anyone clicks on downloaded file, it opens in excel.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
sognoct

use this script instead :

http://forums.asp.net/t/1542176.aspx/1
ASKER CERTIFIED SOLUTION
Rouchie

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
searchsanjaysharma

ASKER
tx