Excel not downloading from asp.net application

BeUnique 2,212 Reputation points
2024-07-30T17:25:58.1666667+00:00

I am developing asp.net application and download excel file not working recently in Local and IIS Website.

The same was working last week.

Below is my code and tell me what is the problem and how to solve this..

This is very urgent

using Excel = Microsoft.Office.Interop.Excel;

using ClosedXML.Excel;

string FileName = "Student Report_" + DateTime.Now + ".xlsx";

using (XLWorkbook wb = new XLWorkbook())

            {

                wb.Worksheets.Add(studentDT, "StudentReport");

                Response.Clear();

                Response.Buffer = true;

                Response.Charset = "";

                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

                Response.AddHeader("content-disposition", "attachment;filename= " + FileName);

                using (MemoryStream MyMemoryStream = new MemoryStream())

                {

                    wb.SaveAs(MyMemoryStream);

                    MyMemoryStream.WriteTo(Response.OutputStream);

                    Response.Flush();

                    Response.End();

                }

            }
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,520 questions
{count} votes

Accepted answer
  1. Michael Taylor 55,216 Reputation points
    2024-08-01T02:39:01.74+00:00

    You didn't really tell us what is going wrong. If it isn't downloading your file then either you're getting an error (which you need to share with us) or there is something wrong elsewhere. Step through your code line by line until either the error occurs or you get to the end.

    Also take a look on the browser side. Presumably you're clicking a link or something to trigger the browser to download the file. Use the Developer Tools (F12) in the browser and look at the network tab. What response is sent back from the browser for this request? Specifically you're interested in the status code, content length, headers, etc. I wonder if it is a browser issue and not related to your code at all.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.