Solved

export to excel doesnt save the file with report.xls

Posted on 2012-03-11
3
457 Views
Last Modified: 2012-06-27
protected void btnexport_Click(object sender, EventArgs e)
    {
        ExportToExcel("Report.xls", GridView1);
        btnexport.Enabled = false;
    }
    public override void VerifyRenderingInServerForm(Control control)
    {
    }




    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
3 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 37711218
The ExportToExcel mejhod simply CREATES the Excel file, it does not Save the file to disk.  That is an action which the user must take for themselves, because they then have the choice of where on disk to actually save the xls file.

AW
0
 
LVL 17

Accepted Solution

by:
ToAoM earned 500 total points
ID: 37711317
Add a Content-Disposition header to the response.

Response.AddHeader("Content-Disposition", "attachment; filename=<file name.ext>");

This should trigger the SaveAs dialog with the supplied file name.

See also: http://www.jtricks.com/bits/content_disposition.html
0
 

Author Closing Comment

by:searchsanjaysharma
ID: 37713112
Thnx
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Default parameter problem in C# 3 27
Image(2) 3 26
Image(7) 1 37
Events in static methods 3 25
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

706 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now