Solved

export to excel doesnt save the file with report.xls

Posted on 2012-03-11
3
462 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
[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
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
ado.net AddWithValue when using IN condition 7 45
Convert VB web project to C# 3 66
vb.net dbnull syntax 1 58
Pdf not loading 2 39
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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