【问题标题】:ASP.NET Excel file download on button click单击按钮时下载 ASP.NET Excel 文件
【发布时间】:2011-10-16 06:01:25
【问题描述】:

首先,所有这些都发生在本地 Intranet 上,在任何时候都不需要连接到 Internet。

我有一个运行查询的数据库,之后用户按下“下载电子表格”按钮创建/发送电子表格。电子表格的创建工作正常,但经过多次尝试后我无法下载文件。这是我尝试过的:

  • 修改响应/标题对象
    • 传输文件
    • 写文件
    • 二进制流
    • 重定向
  • Javascript 重定向
    • Response.Write(javascript 代码)

在大多数情况下,结果是创建了 excel 文件,但没有发生重定向/下载。在 Response.Redirect() 的情况下,如果它是一个网站,它工作得很好,但如果它是一个重定向到 file:///,那么它会抛出一个线程异常但没有更多细节。

我怀疑这与 ASP.NET 文档的生命周期有关,但恐怕我对 ASP.NET 的经验不够,无法确定。

【问题讨论】:

  • 您的响应中的 ContentType 是否设置为 application/vnd.ms-excel?
  • 您使用的是什么版本的 ASP.NET / .NET 框架?
  • 内容类型是,.NET 4

标签: c# asp.net file excel download


【解决方案1】:
FileInfo file = new FileInfo(PathToExcelFile);
if (file.Exists)
{
   Response.Clear();
   Response.ClearHeaders();
   Response.ClearContent();
   Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
   Response.AddHeader("Content-Type", "application/Excel");
   Response.ContentType = "application/vnd.xls";
   Response.AddHeader("Content-Length", file.Length.ToString());
   Response.WriteFile(file.FullName);
   Response.End();
}
else
{
   Response.Write("This file does not exist.");
}

【讨论】:

  • 这是我最初尝试的方法,因为它似乎是“正确”的方法,但 Response.End() 会引发线程中止错误。
  • 嗯...这是我从适合我的生产系统复制的代码。我想知道为什么它不适合你。我会试着弄清楚 Response.End() 发生了什么。
  • 使用 CompleteRequest 方法确实避免了错误,但没有下载文件。
【解决方案2】:
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=Academicprofileexcel.xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            using (StringWriter sw = new StringWriter())
            {
                HtmlTextWriter hw = new HtmlTextWriter(sw);

                //To Export all pages
                Gridview1.AllowPaging = false;
                this.getdetails();

                Gridview1.HeaderRow.BackColor = Color.White;
                foreach (TableCell cell in Gridview1.HeaderRow.Cells)
                {
                    cell.BackColor = Gridview1.HeaderStyle.BackColor;
                }
                foreach (GridViewRow row in Gridview1.Rows)
                {
                    row.BackColor = Color.White;
                    foreach (TableCell cell in row.Cells)
                    {
                        if (row.RowIndex % 2 == 0)
                        {
                            cell.BackColor = Gridview1.AlternatingRowStyle.BackColor;
                        }
                        else
                        {
                            cell.BackColor = Gridview1.RowStyle.BackColor;
                        }
                        cell.CssClass = "textmode";
                    }
                }

                Gridview1.RenderControl(hw);

                //style to format numbers to string
                string style = @"<style> .textmode { } </style>";
                Response.Write(style);
                Response.Output.Write(sw.ToString());
                Response.Flush();
                Response.End();
            }

【讨论】:

    【解决方案3】:
    ClientScript.RegisterStartupScript(GetType(), "hwa", "window.open('" + System.Configuration.ConfigurationManager.AppSettings["WebSite"].ToString() + "Document/SummaryReport/" + FileName + "','_blank');", true);
    

    代码是按钮单击时下载的 Excel 文件。所以现在可以很容易地使用 c# 代码下载您的 excel 文件。

    【讨论】:

    • 你能详细说明你的答案吗?仅发布代码是不够的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    • 1970-01-01
    • 2019-04-29
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 2018-11-27
    相关资源
    最近更新 更多