Search code examples
epplus

Deleting the previously downloaded excel sheet and download new one


I have an application which downloaded excel sheet on button click, the problem is the downloaded excel sheet tells how many times it was downloaded, like, if the sheet name is ExtractReport.xlsx, the after downloading I get ExtractReport(10).xlsx if i have downloaded it 10th time.

Here is the code:

Response.ClearContent();
Response.BinaryWrite(excelExtract.GenerateExtractExcel());
Response.AppendHeader("content-disposition", "attachment; filename = Extract Report.xlsx");
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.Flush();
Response.End();[excel sheet downloaded screen shot][1]

Solution

  • As stated in the comments, this cannot be done as the name is changed by the OS on the client.

    I would suggest generating a new file name based on date and time to avoid the problem. You can use something like the below;

    Response.AppendHeader("content-disposition", $"attachment; filename = Extract Report {DateTime.Now:yyyy-MM-dd - HH.mm}.xlsx");