单击按钮上的ASP.NET Excel文件下载

时间:2011-07-29 20:15:08

标签: c# asp.net file excel download

首先,所有这些都发生在本地内部网上,在任何时候都不需要连接到互联网。

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

  • 修改Response / Header对象
    • 的TransmitFile
    • WriteFile的
    • BinaryStream
    • 重定向
  • Javascript重定向
    • Response.Write(javascript code)

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

我怀疑它与ASP.NET文档的生命周期有关,但我担心我对ASP.NET的经验不足以确定无疑。

3 个答案:

答案 0 :(得分:16)

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.");
}

答案 1 :(得分:0)

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

答案 2 :(得分:-2)

ClientScript.RegisterStartupScript(GetType(), "hwa", "window.open('" + System.Configuration.ConfigurationManager.AppSettings["WebSite"].ToString() + "Document/SummaryReport/" + FileName + "','_blank');", true);

是按钮点击代码是Excel文件下载吗?所以现在可以使用c#代码轻松下载excel文件。

相关问题