How do I programmatically kill a MS Excel process within my C# application that created and started the process

I've written a C# application that uses Microsoft.Office.Interop.Excel.  Application creates an Excel spreadsheet, saves it and then tries to close and quite Excel but the Excel process is still running in the Task Manager.  (same code works in a console app or Windows Forms application but not in my web app).

Here is the basis of the code:

using MyExcel = Microsoft.Office.Interop.Excel;
MyExcel.Application xlApp;
MyExcel.Workbook xlWorkBook;
MyExcel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
 xlApp = new MyExcel.Application();
 xlWorkBook = xlApp.Workbooks.Add(misValue);
 xlApp.DisplayAlerts = false;
 xlWorkSheet = (MyExcel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
 xlWorkSheet.Name = "Daily Report";
xlWorkBook.SaveAs(myFile, MyExcel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, MyExcel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nicolas LecanuAnalyst developerCommented:
Hi fbroccoli,

You need to use the function "Application.Quit" method(Excel)

Here a exemple of this function.
fbroccoliAuthor Commented:
My code contains:  xlApp.Quit();

Is my syntax incorrect?
Nicolas LecanuAnalyst developerCommented:
You are right, sorry.

Can you try this function ?

    private void KillExcel()
        Process[] AllProcesses = Process.GetProcessesByName("excel");

        // check to kill the right process
        foreach ( Process ExcelProcess in AllProcesses) {
        if (myHashtable.ContainsKey(ExcelProcess.Id) == false)

        AllProcesses = null;

Open in new window

Ryan ChongSoftware Team LeadCommented:
you can read this question which provide several options to do that:

C# Visual Studio question regarding how to kill Excel interop process.
fbroccoliAuthor Commented:
Good morning Nicolas,

I have copied and pasted the function into my code.  What do I replace "MyHashtable" with so I can rebuild my application and test it?

Thank you,
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.