Solved

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

Posted on 2012-12-29
6
295 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 44

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 44

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
Industry Leaders: 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 500 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

632 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