Excel app object is Com component so to release we need to execute special code
// 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!
if you want to avoid unmanaged resource issues and work with completely managed code that is not using
Excel Interop to manipulate Excel documents, I recommend you take a look at this
Excel .NET library.
Here is a sample
Excel ASP.NET code that exports DataTable as Excel document to ASP.NET output stream:
// Get DataTable that DataGrid is bound to.
var dataTable = (DataTable)dataGrid.DataSource;
// Create new ExcelFile.
var ef = new ExcelFile();
// Add new worksheet to the file.
var ws = ef.Worksheets.Add(dataTable.TableName);
// Insert the data from DataTable to the worksheet starting at cell "A1".
ws.InsertDataTable(dataTable, "A1", true);
// Stream file to browser.
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.xls");
ef.SaveXls(Response.OutputStream);
Response.End();
Member
130 Points
295 Posts
Excel problem
Aug 02, 2010 06:05 AM|KeyurN|LINK
I m operning excel file using Applicaion object of Excel,Interop
Now i am closing it but it doesn't release from memory
It's process is runnig like Excel.exe
How can i release it.
Keyur N
Contributor
3760 Points
751 Posts
Re: Excel problem
Aug 02, 2010 06:28 AM|prasadP|LINK
Hi Friend,
Excel app object is Com component so to release we need to execute special code
// 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!
http://www.eggheadcafe.com/articles/20021012.asp
http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects-in-c
prasadP
Blog
Participant
1911 Points
443 Posts
Re: Excel problem
Aug 02, 2010 07:01 AM|beharavenkata@gmail.com|LINK
Try below
Marshal.ReleaseComObject(excelobject);
excelobject=null;
Nanda
Member
130 Points
295 Posts
Re: Excel problem
Aug 04, 2010 02:09 AM|KeyurN|LINK
It's not work for me
any other way.
Keyur N
All-Star
67392 Points
6758 Posts
Re: Excel problem
Aug 06, 2010 04:24 AM|Vince Xu - MSFT|LINK
Could you please post some code what you used?
Member
130 Points
295 Posts
Re: Excel problem
Aug 28, 2010 03:25 AM|KeyurN|LINK
oWB.Close(null, null, null);
oXL.Workbooks.Close();
oXL.Quit();
Marshal.ReleaseComObject(oRng);
Marshal.ReleaseComObject(oXL);
Marshal.ReleaseComObject(oSheet);
Marshal.ReleaseComObject(oWB);
try
{
oSheet = null;
oWB = null;
oXL = null;
GC.Collect();
// GC.WaitForPendingFinalizers();
}
catch
{
}
Keyur N
Member
10 Points
21 Posts
Re: Excel problem
Aug 31, 2010 04:39 AM|CikaPero|LINK
Hi,
if you want to avoid unmanaged resource issues and work with completely managed code that is not using Excel Interop to manipulate Excel documents, I recommend you take a look at this Excel .NET library.
Here is a sample Excel ASP.NET code that exports DataTable as Excel document to ASP.NET output stream: