簡體   English   中英

如何從aspx網頁下載excel文件

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

我正在嘗試從aspx網頁上獲取數據庫中以字節為單位存儲的excel文件,然后下載並以.xls格式打開,但是該文件正以.aspx格式下載。 如何解決呢?

我試過了:

    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();
        }
    }

您需要設置Content-Disposition標頭。 例如:

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

並在代碼中:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM