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();
    }
searchsanjaysharmaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
RouchieConnect With a Mentor Commented:
Just add this extra line in amongst your existing code:

Response.AddHeader("content-disposition", "attachment;filename=exported.xls")

Open in new window

0
 
AndyAinscowFreelance programmer / ConsultantCommented:
>>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.
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
ps.  If you wanted an xls file then is writing to a HTMLTextWriter a good idea?
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
searchsanjaysharmaAuthor Commented:
The problem is i want .xls extension, so that whenever anyone clicks on downloaded file, it opens in excel.
0
 
sognoctCommented:
use this script instead :

http://forums.asp.net/t/1542176.aspx/1
0
 
searchsanjaysharmaAuthor Commented:
tx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.