Hi, I have problem to terminate the process EXCEL.EXE after I write into it. After the program finish execute. The EXCEL.EXE is still in the proceses list in the task manager. Anyway to terminate it?

Below is my coding:

SaveFileDialog s_Lvl_Max = new SaveFileDialog();
s_Lvl_Max.Filter = "xls files (*.xls)|*.xls|All files (*.*)|*.*";
s_Lvl_Max.FilterIndex = 1;
s_Lvl_Max.FileName = "FileKK" + "_" + comboBox_baseTP.Text;
s_Lvl_Max.OverwritePrompt = true;

if (s_Lvl_Max.ShowDialog() == DialogResult.OK)
{
                        Excel.Application oXL;
                        Excel._Workbook oWB;
                        Excel._Worksheet oSheet;
                        Excel.Range oRng;

                        try
                        {
                            //Start Excel and get Application object.
                            oXL = new Excel.Application();
                            oXL.Visible = false;
                            oXL.DisplayAlerts = false;

                            //Get a new workbook.
                            oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
                            oSheet = (Excel._Worksheet)oWB.Worksheets[1];

                            //Add table headers going cell by cell.
                            oSheet.Cells[2, 1] = "Level (Max VCC) Guardband";
                            oSheet.Cells[4, 1] = "Test Name";
                            oSheet.Cells[4, 2] = "Frequency";
                            oSheet.Cells[4, 3] = "[Spec]";
                            oSheet.Cells[4, 4] = "Actual";
                            oSheet.Cells[4, 5] = "temp(V)";

                            //Format A1:D1 as bold, vertical alignment = center.
                            oSheet.get_Range("A1", "E1").Font.Bold = true;
                            oSheet.get_Range("A1", "E1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;

                            // Create an array to multiple values at once.
                            int i = 5;

                            foreach (DataRow dr in dtable2.Rows)
                            {
                                oSheet.Cells[i, 1] = dr[0];
                                oSheet.Cells[i, 2] = dr[1];
                                oSheet.Cells[i, 3] = dr[2];
                                oSheet.Cells[i, 4] = dr[3];
                                oSheet.Cells[i, 5] = dr[4];

                                i++;
                            }

                            i--;

                            //AutoFit columns A:D.
                            oRng = oSheet.get_Range("A1", "E1");
                            oRng.EntireColumn.AutoFit();

                            oWB.SaveAs(s_Lvl_Max.FileName, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
                            Missing.Value, Excel.XlSaveAsAccessMode.xlExclusive, Missing.Value,
                            Missing.Value, Missing.Value, Missing.Value, Missing.Value);
                            oXL.Quit(); // <--------- try to use this to terminate the process, not working 
                            MessageBox.Show("File has been successfully saved. :)");
}

Try this... Not working either....

// Need all following code to clean up and extingush all references!!!
oWB.Close(null,null,null);
oXL.Workbooks.Close();
oXL.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject (oRng);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oXL);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oSheet);
System.Runtime.InteropServices.Marshal.ReleaseComObject (oWB);
oSheet=null;
oWB=null;
oXL = null;
GC.Collect(); // force final cleanup!

It may be over a month since you posted but for what it's worth I ran across the same issue and could not find any help on the web so I came up with this code that does the trick. This code loops through all the Excel processes and when it finds the one with the same window title as the application you started it ends that process then exits the loop.

    using System.Diagnostics;

    foreach (Process p in Process.GetProcessesByName("EXCEL"))
    {
        if (oXL.Caption == p.MainWindowTitle)
        {
            p.Kill();
            break;
        }
     }

Edited 3 Years Ago by Nick Evan: Fixed formatting

Yeah I guess there's no complete solution for this problem. I found another solution which seems working as well:

// Need all following code to clean up and extingush all references!!!
            oWB.Close(null, null, null);
            oXL.Workbooks.Close();
            Process[] pProcess;
            pProcess = System.Diagnostics.Process.GetProcessesByName("Excel");
            pProcess[0].Kill();

            oSheet = null;
            oWB = null;
            oXL = null;

Thanks anyway guys for all the help given!!

This article has been dead for over six months. Start a new discussion instead.