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();
KeyurN
Member
105 Points
201 Posts
Excel problem
Aug 02, 2010 10:05 AM|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
prasadP
Contributor
5266 Points
771 Posts
Re: Excel problem
Aug 02, 2010 10:28 AM|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
beharavenkat...
Contributor
2799 Points
448 Posts
Re: Excel problem
Aug 02, 2010 11:01 AM|LINK
Try below
Marshal.ReleaseComObject(excelobject);
excelobject=null;
Nanda
KeyurN
Member
105 Points
201 Posts
Re: Excel problem
Aug 04, 2010 06:09 AM|LINK
It's not work for me
any other way.
Keyur N
Vince Xu - M...
All-Star
80367 Points
6801 Posts
Re: Excel problem
Aug 06, 2010 08:24 AM|LINK
Could you please post some code what you used?
KeyurN
Member
105 Points
201 Posts
Re: Excel problem
Aug 28, 2010 07:25 AM|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
CikaPero
Member
54 Points
36 Posts
Re: Excel problem
Aug 31, 2010 08:39 AM|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:
// 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();John Flor
Member
22 Points
8 Posts
Re: Excel problem
Jun 16, 2011 08:19 AM|LINK
It's known Excel problem. Microsoft Excel API is a COM library with bad integration with .NET Framework.
I've worked with Excel Jetcell .NET for a long time and can really recommend it.
At Excel Sample page you will find many useful C# VB.NET Excel codes, for instance:
// Create New Excel Workbook in C# ExcelWorkbook Wbook = new ExcelWorkbook(); ExcelCellCollection Cells = Wbook.Worksheets.Add("Sheet1").Cells; Cells["A1"].Value = "Excel writer example (C#)"; Cells["A1"].Style.Font.Bold = true; Cells["B1"].Value = "=550 + 5"; // Write Excel XLS file Wbook.WriteXLS("excel_net.xls");