Solved

export to excel doesnt save the file with report.xls

Posted on 2012-03-11
3
458 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

13 Experts available now in Live!

Get 1:1 Help Now