Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 474
  • Last Modified:

export to excel doesnt save the file with report.xls

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
searchsanjaysharma
Asked:
searchsanjaysharma
1 Solution
 
Arthur_WoodCommented:
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
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
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
 
searchsanjaysharmaAuthor Commented:
Thnx
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now