简体   繁体   English

如何从aspx网页下载excel文件

[英]how to download an excel file from aspx webpage

I am trying to fetch an excel file stored in bytes from database from an aspx webpage and download and open in .xls format, however the file is getting downloaded as .aspx . 我正在尝试从aspx网页上获取数据库中以字节为单位存储的excel文件,然后下载并以.xls格式打开,但是该文件正以.aspx格式下载。 How to resolve this? 如何解决呢?

I tried: 我试过了:

    private void download(DataTable dt)
    {


        if (dt.Rows.Count > 0)
        {
            Byte[] bytes = (Byte[])dt.Rows[0]["xyz"];
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.ms-excel";
            Response.BinaryWrite(bytes);
           // Response.Flush();
            Response.End();
        }
    }

You need to set the Content-Disposition header. 您需要设置Content-Disposition标头。 For example: 例如:

Content-Disposition: attachment; filename=your-excel-file.xlsx

And in code: 并在代码中:

Response.AddHeader("Content-Disposition", "attachment; filename=your-excel-file.xlsx");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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