Calling the
SaveAs
method multiple times will have unpredictable results. Instead, save the package on the server, and then use the
Response.TransmitFile
method to send the file to the client:
string filename = string.Format(@"REPORT_{0:dd-MM-yyyy_hh-mm-ss}.xlsx", datetime);
var file = new FileInfo(Path.Combine(@"C:\Users\testacc\Desktop\Test\", filename));
package.SaveAs(file);
Response.Clear();
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=" + filename + ";");
Response.TransmitFile(file.FullName);
Application.CompleteRequest();