Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-12-29
6
Medium Priority
?
298 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
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

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!

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

670 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