简体   繁体   中英

Save Window at Excel Report in C#

I have a working Excel Report code. Still, when I click the button the file goes directly to my download folder, without giving me the option to change its name or selecting where I want to save it.

Code is the following:

public void GetExcel()
{
    var list = (IList<LeaseViewModel>)Session["currentList"];
    var grd = new GridView { DataSource = list, AutoGenerateColumns = true };
    grd.DataBind();
    Response.ClearContent();
    Response.AddHeader("Content-type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    Response.AddHeader("content-disposition", "attachment;filename=Leases.xls");
    Response.ContentType = "application/excel";
    var swr = new StringWriter();
    var tw = new HtmlTextWriter(swr);
    grd.RenderControl(tw);
    Response.Write(swr.ToString());
    Response.Flush();
    Response.End();
    tw.Close();
    swr.Close();
}

Can somebody please indicate what should I change in order to have the window popping?

Thanks a lot.

Thanks for everybody highlighting this is a browser-configuration issue.

I thought that once I have the definition of the default name in the code I could also define it to always ask what is the name I want. I tested in different browsers though, and fact is you were right, thanks for identifying it and noticing me.

Regarding the default name issue, here is how I solve it:

Before:

Response.AddHeader("content-disposition", "attachment;filename=Leases.xls");

After:

Response.AddHeader("content-disposition", "attachment;filename=" + Session["listName"] + "_Report.xls");

Now regardless the report I export, the name is always customized.

Hope it helps other newbies like me (:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM