0

I am trying to export some data to excel and download the file via the browser. I have a method that creates an active workbook that returns a byte array in the following way:

byte[] doc = be.GetActiveWorkbook(excelApp);

The method GetActiveWorkBook looks like this:

public byte[] GetActiveWorkbook(Application app)
{
        string path = Path.GetTempFileName();

        try
        {
            app.ActiveWorkbook.SaveCopyAs(path);
            return File.ReadAllBytes(path);
        }
        finally
        {
            if (File.Exists(path))
                File.Delete(path);
        }
}

Lastly, the file is returned like this:

var file = File(doc, "application/vnd.ms-excel");
file.FileDownloadName = filename + " " + id + ".xlsx";
return file;

The excel file is indeed downloaded to the browser, however it seems like there are processes in the background that are still active, even though I close the excel file in my desktop. Why is this?

jazy
  • 75
  • 4
  • 14

1 Answers1

1

The Excel Interop is a nightmare for disposing of properly. Probably worth checking your code outside of the method you've posted to see how you're cleaning up the Excel Application that's passed in:

How do I properly clean up Excel interop objects?

Tom John
  • 783
  • 5
  • 14
  • I did not know there was a post on this issue already. Thank you, this solved my problem! – jazy Sep 20 '17 at 12:19