Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel Automation closing the process

Status
Not open for further replies.

djam

Technical User
Nov 15, 2002
223
CA
I can't seem to close down Excel when I'm done with it. It remains in the task manager process.

Here is some code snippit:

Excel.Application oXL;
Excel._Workbook oWB;
oXL = new Excel.Application();
oWB = (Excel._Workbook)(oXL.Workbooks.Open(filePath.ToString(),missing,false,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing));

oWB.Close(false,missing,missing);
oXL.Workbooks.Close();
oXL.Quit();
oWB = null;
oXL = null;

" ahhh computers, how they made our lives much simpler ;) "
 
Try to add the followings calls after Quit() and also run the release version of the app.
Code:
System.Runtime.InteropServices.Marshal.ReleaseComObject(oXL));
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWB);
GC.Collect();
obislavu

 
It didn't work, the process is still running in the task manager

" ahhh computers, how they made our lives much simpler ;) "
 
yep

" ahhh computers, how they made our lives much simpler ;) "
 
try to force a gargage collection cycle by:

GC.Collect();

Beware that this can be a very expensive call, but I've found that it will kill that hanging ExHell process.

-p

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Here is an example on how I am using Excel to export data to an .xls file.
This code is working well and should work for you!
Code:
private object m_MissingValue = System.Reflection.Missing.Value;
private Excel.Application m_Excel;
private Excel._Workbook m_WorkBook;
private Excel._Worksheet m_WorkSheet;

private void Export_Click(object sender, System.EventArgs e)
{
	
	try 
	{
		GC.Collect(); 
		m_Excel = new Excel.Application();
		m_WorkBook= m_Excel.Workbooks.Add(m_MissingValue);
		Excel._Worksheet w = (Excel._Worksheet)m_WorkBook.Worksheets.get_Item(1);
		m_WorkSheet=(Excel._Worksheet) ((m_WorkBook.Worksheets.Count>0 ) ? m_WorkBook.Worksheets.get_Item(1) : m_WorkBook.Worksheets.Add(m_MissingValue,m_MissingValue,m_MissingValue,m_MissingValue)); 
		m_WorkSheet.Name =m_SearchForm.SelectedInventoryView.Name;
		// Populate With Data
		//DataSet vDataSet = GetDataSet("SELECT * FROM ..." ;
		//int r=0,c=0;
		//foreach(DataColumn vDataColumn in vDataSet.Tables[0].Columns)
		//{
		//	m_WorkSheet.Cells[1,c+1]=vDataColumn.ColumnName;
		//	c++;
		//}
		//r++;
		//..
		m_WorkBook.SaveAs("~myfile.xls", m_MissingValue,m_MissingValue, m_MissingValue, m_MissingValue, Excel.XlSaveAsAccessMode.xlNoChange, m_MissingValue, m_MissingValue, m_MissingValue, m_MissingValue, m_MissingValue);
		m_WorkBook.Close(null, null, null);
		m_Excel.Quit();
	}
	catch (Exception ex)
	{
		MessageBox.Show("Unable to export data. An error ocurred when launching the Excel application. " + ex.GetType() +ex.Message); 
	}
	finally
	{
		if (m_Excel!=null) System.Runtime.InteropServices.Marshal.ReleaseComObject(m_Excel);
		if (m_WorkSheet!=null) System.Runtime.InteropServices.Marshal.ReleaseComObject(m_WorkSheet);
		if (m_WorkBook!=null) System.Runtime.InteropServices.Marshal.ReleaseComObject(m_WorkBook);
		m_WorkSheet=null;
		m_WorkBook=null;
		m_Excel=null;
		GC.Collect(); 
	
	}
	
}
obislavu
 
I recently had to do some c# Excel integration and came across a few of these issues too.
I have found that if there are any excel objects which have not been set to NULL, the Excel instance will remain. So if you have declared an application and workbook object and not set it both to NULL, the application will remain valid.
Also, the object is only killed off once you have exited the scope in which the object is declared, so if it is a global type declaration it does not finish until the c# application does.
I never delved into it too deeply so do not take what I say here as gospepl, but it is a starting point.
I hope all of this helps.

-- Gavin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top