Hi All
I'm trying to create a program that writes some data to excel and creates a graph. Currently when I am finished with the COM I close it by doing the following:
if (xla != null)
{
xla.Quit();
xla = null; // Ends connection
}
Is this what I should be doing? I don't think it is, as every time I run the program it opens another Excel process that does not close after I have finished with the COM. What should I be doing? Your thoughts and tips are appreciated!
Cameron
Loading

Roei BarRank #1,271 · 1.4K reputationPosted Sep 2, 2009, 11:37 PM EDT
here is the code to do the job :
using System.Diagnostics; using System.Runtime.InteropServices; Excel.Application excelApp = null; Excel.Workbook excelWorkbook = null; Excel.Sheets excelSheets = null; Excel.Worksheet excelWorksheet = null; Excel.Workbooks excelWorkbooks = null; private void button3_Click(object sender, System.EventArgs e) { excelApp = new Excel.ApplicationClass(); excelApp.Visible = true; excelWorkbooks = excelApp.Workbooks; excelWorkbook = excelWorkbooks.Add(System.Reflection.Missing.Value); excelSheets = excelWorkbook.Worksheets; excelWorksheet = (Excel.Worksheet) excelSheets.get_Item(1); excelWorksheet.Cells[1,1] = "42"; } private void button4_Click(object sender, System.EventArgs e) { excelWorkbook.Close (false, System.Reflection.Missing.Value,System.Reflection.Missing.Value) ; excelWorkbooks.Close(); excelApp.Quit(); Marshal.ReleaseComObject(excelWorksheet); Marshal.ReleaseComObject(excelSheets); Marshal.ReleaseComObject(excelWorkbooks); Marshal.ReleaseComObject(excelWorkbook); Marshal.ReleaseComObject(excelApp); excelWorksheet = null; excelSheets = null; excelWorkbooks = null; excelWorkbook = null; excelApp = null; GC.GetTotalMemory(false); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.GetTotalMemory(true); Process[] pProcess; pProcess = System.Diagnostics.Process.GetProcessesByName("Excel"); if (pProcess.Length > 0) { pProcess[0].Kill(); } }Cameron9 reputationPosted Sep 3, 2009, 12:29 AM EDT
Roei BarRank #1,271 · 1.4K reputationPosted Sep 3, 2009, 3:37 AM EDT
so i am afraid so