C# problem. Process Excel.exe not killed

Hi,

I’m new to C#. I’m writing a program that open a new excel file, write into it and lastly close the file. But the program is that there will be a orphaned process named EXCEL.EXE left on the process queue whenever the program quit. Anyway to terminate it automatically when the application exit? I try to use Marshal.ReleaseCOMObject and Garbage Collection. None of these works. Here’s my coding:

[CODE=csharp]
[SIZE=2][COLOR=#2b91af]SaveFileDialog[/COLOR][/SIZE][SIZE=2] s_Lvl_Max = [/SIZE][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#2b91af]SaveFileDialog[/COLOR][/SIZE]SIZE=2;
s_Lvl_Max.Filter = [/SIZE][SIZE=2][COLOR=#a31515]“xls files (.xls)|.xls|All files (.)|.”[/COLOR][/SIZE][SIZE=2];
s_Lvl_Max.FilterIndex = 1;
s_Lvl_Max.FileName = [/SIZE][SIZE=2][COLOR=#a31515]“filenamekk”[/COLOR][/SIZE][SIZE=2] + [/SIZE][SIZE=2][COLOR=#a31515]"_"[/COLOR][/SIZE][SIZE=2] + comboBox_baseTP.Text;
s_Lvl_Max.OverwritePrompt = [/SIZE][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][SIZE=2];
[/SIZE][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][SIZE=2] (s_Lvl_Max.ShowDialog() == [/SIZE][SIZE=2][COLOR=#2b91af]DialogResult[/COLOR][/SIZE][SIZE=2].OK)
{
Excel.[/SIZE][SIZE=2][COLOR=#2b91af]Application[/COLOR][/SIZE][SIZE=2] oXL;
Excel.[/SIZE][SIZE=2][COLOR=#2b91af]_Workbook[/COLOR][/SIZE][SIZE=2] oWB;
Excel.[/SIZE][SIZE=2][COLOR=#2b91af]_Worksheet[/COLOR][/SIZE][SIZE=2] oSheet;
[/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]try
[/COLOR][/SIZE][SIZE=2]{
[/SIZE][SIZE=2][COLOR=#008000]//Start Excel and get Application object.
[/COLOR][/SIZE][SIZE=2]oXL = [/SIZE][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][SIZE=2] Excel.[/SIZE][SIZE=2][COLOR=#2b91af]Application[/COLOR][/SIZE]SIZE=2;
oXL.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][SIZE=2];
oXL.DisplayAlerts = [/SIZE][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][SIZE=2];
[/SIZE][SIZE=2][COLOR=#008000]//Get a new workbook.
[/COLOR][/SIZE][SIZE=2]oWB = (Excel.[/SIZE][SIZE=2][COLOR=#2b91af]_Workbook[/COLOR][/SIZE][SIZE=2])(oXL.Workbooks.Add([/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value));
oSheet = (Excel.[/SIZE][SIZE=2][COLOR=#2b91af]_Worksheet[/COLOR][/SIZE][SIZE=2])oWB.Worksheets[1];
[/SIZE][SIZE=2][COLOR=#008000]//Add table headers going cell by cell.
[/COLOR][/SIZE][SIZE=2]oSheet.Cells[2, 1] = [/SIZE][SIZE=2][COLOR=#a31515]"Level "[/COLOR][/SIZE][SIZE=2];
oSheet.Cells[4, 1] = [/SIZE][SIZE=2][COLOR=#a31515]“Test”[/COLOR][/SIZE][SIZE=2];
oSheet.Cells[4, 2] = [/SIZE][SIZE=2][COLOR=#a31515]“Frequency”[/COLOR][/SIZE][SIZE=2];
oSheet.Cells[4, 3] = [/SIZE][SIZE=2][COLOR=#a31515]“Spec”[/COLOR][/SIZE][SIZE=2];
oSheet.Cells[4, 4] = [/SIZE][SIZE=2][COLOR=#a31515]“Actual”[/COLOR][/SIZE][SIZE=2];
oSheet.Cells[4, 5] = [/SIZE][SIZE=2][COLOR=#a31515]“Guard”[/COLOR][/SIZE][SIZE=2];
[/SIZE][SIZE=2][COLOR=#008000]//Format A1:D1 as bold, vertical alignment = center.
[/COLOR][/SIZE][SIZE=2]oSheet.get_Range([/SIZE][SIZE=2][COLOR=#a31515]“A1”[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515]“E1”[/COLOR][/SIZE][SIZE=2]).Font.Bold = [/SIZE][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][SIZE=2];
oSheet.get_Range([/SIZE][SIZE=2][COLOR=#a31515]“A1”[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515]“E1”[/COLOR][/SIZE][SIZE=2]).VerticalAlignment = Excel.[/SIZE][SIZE=2][COLOR=#2b91af]XlVAlign[/COLOR][/SIZE][SIZE=2].xlVAlignCenter;
[/SIZE][SIZE=2][COLOR=#008000]// Create an array to multiple values at once.
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]int[/COLOR][/SIZE][SIZE=2] i = 5;
[/SIZE][SIZE=2][COLOR=#0000ff]foreach[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af]DataRow[/COLOR][/SIZE][SIZE=2] dr [/SIZE][SIZE=2][COLOR=#0000ff]in[/COLOR][/SIZE][SIZE=2] 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–;
[/SIZE][SIZE=2][COLOR=#008000]//AutoFit columns A:D.
[/COLOR][/SIZE][SIZE=2]oSheet.get_Range([/SIZE][SIZE=2][COLOR=#a31515]“A1”[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515]“E1”[/COLOR][/SIZE][SIZE=2]).EntireColumn.AutoFit();

oWB.SaveAs(s_Lvl_Max.FileName, [/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value, [/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value, [/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value, [/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value,
[/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value, Excel.[/SIZE][SIZE=2][COLOR=#2b91af]XlSaveAsAccessMode[/COLOR][/SIZE][SIZE=2].xlExclusive, [/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value,
[/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value, [/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value, [/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value, [/SIZE][SIZE=2][COLOR=#2b91af]Missing[/COLOR][/SIZE][SIZE=2].Value);
[/SIZE][SIZE=2][COLOR=#008000]// Need all following code to clean up and extingush all references!!!
[/COLOR][/SIZE][SIZE=2]oWB.Close([/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2]);
oXL.Workbooks.Close();
oXL.Quit();
[/SIZE][SIZE=2][COLOR=#008000]//System.Runtime.InteropServices.Marshal.ReleaseComObject(oRng);
[/COLOR][/SIZE][SIZE=2]System.Runtime.InteropServices.[/SIZE][SIZE=2][COLOR=#2b91af]Marshal[/COLOR][/SIZE][SIZE=2].ReleaseComObject(oXL);
System.Runtime.InteropServices.[/SIZE][SIZE=2][COLOR=#2b91af]Marshal[/COLOR][/SIZE][SIZE=2].ReleaseComObject(oSheet);
System.Runtime.InteropServices.[/SIZE][SIZE=2][COLOR=#2b91af]Marshal[/COLOR][/SIZE][SIZE=2].ReleaseComObject(oWB);
oSheet = [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2];
oWB = [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2];
oXL = [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2];
[/SIZE][SIZE=2][COLOR=#2b91af]GC[/COLOR][/SIZE][SIZE=2].Collect(); [/SIZE][SIZE=2][COLOR=#008000]// force final cleanup!
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#2b91af]MessageBox[/COLOR][/SIZE][SIZE=2].Show(s_Lvl_Max.FileName + [/SIZE][SIZE=2][COLOR=#a31515]" has been successfully saved. :)"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#a31515]“File Saved”[/COLOR][/SIZE][SIZE=2]);
}
[/SIZE][SIZE=2][COLOR=#0000ff]catch[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#2b91af]Exception[/COLOR][/SIZE][SIZE=2] theException)
{
[/SIZE][SIZE=2][COLOR=#2b91af]String[/COLOR][/SIZE][SIZE=2] errorMessage;
errorMessage = [/SIZE][SIZE=2][COLOR=#a31515]"Error: “[/COLOR][/SIZE][SIZE=2];
errorMessage = [/SIZE][SIZE=2][COLOR=#2b91af]String[/COLOR][/SIZE][SIZE=2].Concat(errorMessage, theException.Message);
errorMessage = [/SIZE][SIZE=2][COLOR=#2b91af]String[/COLOR][/SIZE][SIZE=2].Concat(errorMessage, [/SIZE][SIZE=2][COLOR=#a31515]” Line: "[/COLOR][/SIZE][SIZE=2]);
errorMessage = [/SIZE][SIZE=2][COLOR=#2b91af]String[/COLOR][/SIZE][SIZE=2].Concat(errorMessage, theException.Source);
[/SIZE][SIZE=2][COLOR=#2b91af]Console[/COLOR][/SIZE][SIZE=2].WriteLine(errorMessage, [/SIZE][SIZE=2][COLOR=#a31515]“Error”[/COLOR][/SIZE][SIZE=2]);
}
}
[/SIZE]


 
Any other way???