Excel remains in memory
-
I have written the simple code as below: Addede reference (C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11\Microsoft.Office.Interop.Excel.dll) private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application _objAppln; Microsoft.Office.Interop.Excel.Workbook _objWorkBook=null ; _objAppln = new Microsoft.Office.Interop.Excel.Application(); // To initialize excel file if (_objAppln != null) { _objWorkBook = _objAppln.Workbooks.Add(Type.Missing); // To add workbook with sheets in excel file //_objWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)_objWorkBook.ActiveSheet; // To get the current active sheet in excel file } string _fileName = "temp.xls"; _objWorkBook.SaveAs(_fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); _objWorkBook.Close(true, _fileName, false); _objAppln.Quit(); _objWorkBook = null; _objAppln = null; } After the code is execuled, the instance of Excel (EXCEL.exe) is seen in taskbar. I have as many number of instances, as many times I click on the button. The instance exists even when I stop the application. What is wrong in the above code.
Gurpreet
-
I have written the simple code as below: Addede reference (C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11\Microsoft.Office.Interop.Excel.dll) private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application _objAppln; Microsoft.Office.Interop.Excel.Workbook _objWorkBook=null ; _objAppln = new Microsoft.Office.Interop.Excel.Application(); // To initialize excel file if (_objAppln != null) { _objWorkBook = _objAppln.Workbooks.Add(Type.Missing); // To add workbook with sheets in excel file //_objWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)_objWorkBook.ActiveSheet; // To get the current active sheet in excel file } string _fileName = "temp.xls"; _objWorkBook.SaveAs(_fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); _objWorkBook.Close(true, _fileName, false); _objAppln.Quit(); _objWorkBook = null; _objAppln = null; } After the code is execuled, the instance of Excel (EXCEL.exe) is seen in taskbar. I have as many number of instances, as many times I click on the button. The instance exists even when I stop the application. What is wrong in the above code.
Gurpreet
-
Hi I had the same problem
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();//excel will close only after calling these methods twice
GC.WaitForPendingFinalizers();after setting _objAppln=null; worked for me greets
Explicitly calling GC.collect is not recommended. Is there any other good way to handle it?
Gurpreet
-
I have written the simple code as below: Addede reference (C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11\Microsoft.Office.Interop.Excel.dll) private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application _objAppln; Microsoft.Office.Interop.Excel.Workbook _objWorkBook=null ; _objAppln = new Microsoft.Office.Interop.Excel.Application(); // To initialize excel file if (_objAppln != null) { _objWorkBook = _objAppln.Workbooks.Add(Type.Missing); // To add workbook with sheets in excel file //_objWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)_objWorkBook.ActiveSheet; // To get the current active sheet in excel file } string _fileName = "temp.xls"; _objWorkBook.SaveAs(_fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); _objWorkBook.Close(true, _fileName, false); _objAppln.Quit(); _objWorkBook = null; _objAppln = null; } After the code is execuled, the instance of Excel (EXCEL.exe) is seen in taskbar. I have as many number of instances, as many times I click on the button. The instance exists even when I stop the application. What is wrong in the above code.
Gurpreet
-
I have written the simple code as below: Addede reference (C:\Program Files\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office11\Microsoft.Office.Interop.Excel.dll) private void button1_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel.Application _objAppln; Microsoft.Office.Interop.Excel.Workbook _objWorkBook=null ; _objAppln = new Microsoft.Office.Interop.Excel.Application(); // To initialize excel file if (_objAppln != null) { _objWorkBook = _objAppln.Workbooks.Add(Type.Missing); // To add workbook with sheets in excel file //_objWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)_objWorkBook.ActiveSheet; // To get the current active sheet in excel file } string _fileName = "temp.xls"; _objWorkBook.SaveAs(_fileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, false, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); _objWorkBook.Close(true, _fileName, false); _objAppln.Quit(); _objWorkBook = null; _objAppln = null; } After the code is execuled, the instance of Excel (EXCEL.exe) is seen in taskbar. I have as many number of instances, as many times I click on the button. The instance exists even when I stop the application. What is wrong in the above code.
Gurpreet
KaurGurpreet wrote:
What is wrong in the above code.
I'm missing the code to close the workbook and exit the Excell application. Meh, they're there, just overlooked them. This kb-article[^] might help. The only other alternative[^] that I can offer;
Marshal.ReleaseComObject(workBook);
I are Troll :suss: