Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2012-12-29
6
Medium Priority
?
300 Views
Last Modified: 2013-01-16
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();
    }
0
Comment
Question by:searchsanjaysharma
6 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38728864
>>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
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 38728865
ps.  If you wanted an xls file then is writing to a HTMLTextWriter a good idea?
0
 

Author Comment

by:searchsanjaysharma
ID: 38728867
The problem is i want .xls extension, so that whenever anyone clicks on downloaded file, it opens in excel.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:sognoct
ID: 38728869
use this script instead :

http://forums.asp.net/t/1542176.aspx/1
0
 
LVL 25

Accepted Solution

by:
Rouchie earned 1500 total points
ID: 38729186
Just add this extra line in amongst your existing code:

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

Open in new window

0
 

Author Comment

by:searchsanjaysharma
ID: 38782081
tx
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question