5

To preface, all of this occurs on a local intranet, which does not at any point need to connect to the internet.

I have a database on which I run a query, after which the user presses a "Download Spreadsheet" button which creates/sends the spreadsheet. The creation of the spreadsheet works fine, but after many attempts I cannot get the file to download. Here's what I've tried:

  • Modifying the Response/Header objects
    • TransmitFile
    • WriteFile
    • BinaryStream
    • Redirect
  • Javascript Redirect
    • Response.Write(javascript code)

In most cases, the result is that the excel file is created, but no redirect/download occurs. In the case of Response.Redirect(), if it's a website it works great, but if it's a redirect to a file:///, then it throws a thread exception but no more detail.

I suspect it has to do with the lifecycle of an ASP.NET document, but I'm afraid I am not experienced enough with ASP.NET to be able to know for sure.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Itskiddly
  • 113
  • 1
  • 1
  • 5

3 Answers3

18
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.");
}
Shane Wealti
  • 2,252
  • 3
  • 19
  • 33
  • This was what I tried at first, as it seems the 'proper' way to do it, but Response.End() throws a thread abort error. – Itskiddly Jul 29 '11 at 20:37
  • Hmm...that's code I copied from a production system which works for me. I wonder why it's not working for you. I'll try to figure out what is happening at Response.End(). – Shane Wealti Jul 29 '11 at 20:39
  • Using the CompleteRequest method did avoid the error, but no file was downloaded. – Itskiddly Jul 29 '11 at 20:47
1
            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
ClientScript.RegisterStartupScript(GetType(), "hwa", "window.open('" + System.Configuration.ConfigurationManager.AppSettings["WebSite"].ToString() + "Document/SummaryReport/" + FileName + "','_blank');", true);

Is that the code is Excel file download on button click. so now it's easy to download your excel file using c# code.

Prince
  • 11
  • 1