Solved

export to excel doesnt save the file with report.xls

Posted on 2012-03-11
3
461 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:
Jesse Houwing 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

828 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