COM Question (Excel)

G

Gunawan

Dear All,

I have create an excel (COM Object) using this code

Excel.Application xls = new Excel.Application();

but I can not remove it from memory although I have using close and quit

wb.Close(false, false, 0);
xls.Quit();


If I open task manager there is excel.exe. If I run multiple time the number
of excel.exe in task manager will increase.

Please Help.

Regards,
Gunawan
 
P

Patrick Steele

Dear All,

I have create an excel (COM Object) using this code

Excel.Application xls = new Excel.Application();

but I can not remove it from memory although I have using close and quit

wb.Close(false, false, 0);
xls.Quit();


If I open task manager there is excel.exe. If I run multiple time the number
of excel.exe in task manager will increase.

What is "wb"? A reference to a workbook I assume?

Make sure you release every COM object you get a reference to using
Marshal.ReleaseComObject()

Excel.Application xls = new Excel.Application();
try
{
wb = ...
try
{
...
}
finally
{
wb.Close(false, false, 0);
Marshal.ReleaseComObject(wb);
}
}
finally
{
xls.Quit();
Marshal.ReleaseComObject(xls);
}
 
G

Gunawan

Hi Patrick,
I still can not release excel.exe from the memory.

Currenty I am using this code on *.aspx file to response click button.

Is it related to this issue? and I am using visual studio 2003.
Marshal.ReleaseComObject still can not remove excel.exe. :(
Regards,
Gunawan
 
R

RobinS

It's more likely that whatever you are doing in Excel is leaving something
open. Are you doing OLE Automation or what? This is a problem *many* people
have when doing OLE Automation with Excel.

For example, I had a problem once and it turned out to be a problem with
Ranges. If you use any kind of Ranges (like
..range(.cells(1,2),.cells(3,4)).merge=true), Excel creates a Range object
behind the scenes, and does not dispose of it. You have to do this:

(Sorry this is VB)

Dim myRange as Range = xlsheet.range(.Cells(1,2),.cells(3,4))
myRange.Merge=True
myRange = Nothing

That's just one example, but it depends on what you're doing. The way I
tracked it down was I commented out every single excel command I was using,
then uncommented them one at a time until I found the culprit. Tedious, but
effective.

So the question is, what are you doing with Excel? Are you just opening it
and closing it, or are you doing something in-between?

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
-----------------------------------------------
Gunawan said:
Hi Patrick,
I still can not release excel.exe from the memory.

Currenty I am using this code on *.aspx file to response click button.

Is it related to this issue? and I am using visual studio 2003.
Marshal.ReleaseComObject still can not remove excel.exe. :(
Regards,
Gunawan
 
G

Gunawan

I will try to explain what I am doing with excel file.

I try to open excel file
Open Excel file
---------------
Application xls = new Application();
_Workbook wb = xls.Workbooks.Open(strFilename,
0, true, 5, "", "", true, Excel.XlPlatform.xlWindows,
"\t", false, false, 0 , true, 0, 0);

_Worksheet wsh = (_Worksheet) wb.Worksheets[1];

Retrieving Data
for
{

strKodeBarang = ((Range) wsh.Cells[i,1]).Value2.ToString() ;
strNamaBarang = ((Range) wsh.Cells[i,2]).Value2.ToString() ;
}


Releasing Object
-----------------
Marshal.ReleaseComObject(wsh);

wb.Close(false, false, 0);
Marshal.ReleaseComObject(wb);

xls.Quit();
Marshal.ReleaseComObject(xls);


I use this tehnique using vb6 and no excel.exe retain on memory.
But using C# always left excel.exe in memory.

Regards,
Gun

RobinS said:
It's more likely that whatever you are doing in Excel is leaving something
open. Are you doing OLE Automation or what? This is a problem *many*
people have when doing OLE Automation with Excel.

For example, I had a problem once and it turned out to be a problem with
Ranges. If you use any kind of Ranges (like
.range(.cells(1,2),.cells(3,4)).merge=true), Excel creates a Range object
behind the scenes, and does not dispose of it. You have to do this:

(Sorry this is VB)

Dim myRange as Range = xlsheet.range(.Cells(1,2),.cells(3,4))
myRange.Merge=True
myRange = Nothing

That's just one example, but it depends on what you're doing. The way I
tracked it down was I commented out every single excel command I was
using, then uncommented them one at a time until I found the culprit.
Tedious, but effective.

So the question is, what are you doing with Excel? Are you just opening it
and closing it, or are you doing something in-between?

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
-----------------------------------------------
 
L

Laurent Bugnion [MVP]

Hi,
Hi Patrick,
I still can not release excel.exe from the memory.

Currenty I am using this code on *.aspx file to response click button.

That's quite a bad idea, regardless of all the other problems you're
getting:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q257757#kb2

There are other ways to deal with Excel on the server. Using ADO.NET ist
a good way, or you can also manipulate Excel files in XML format.

HTH,
Laurent
 
W

Willy Denoyette [MVP]

Gunawan said:
I will try to explain what I am doing with excel file.

I try to open excel file
Open Excel file
---------------
Application xls = new Application();
_Workbook wb = xls.Workbooks.Open(strFilename,
0, true, 5, "", "", true, Excel.XlPlatform.xlWindows,
"\t", false, false, 0 , true, 0, 0);

_Worksheet wsh = (_Worksheet) wb.Worksheets[1];

Retrieving Data
for
{

strKodeBarang = ((Range) wsh.Cells[i,1]).Value2.ToString() ;
strNamaBarang = ((Range) wsh.Cells[i,2]).Value2.ToString() ;
}


Releasing Object
-----------------
Marshal.ReleaseComObject(wsh);

wb.Close(false, false, 0);
Marshal.ReleaseComObject(wb);

Should be...
xls.Quit();
GC.Collect();
GC.WaitForPendingFinalizers();

Anyway, you should know that this is an unsupported scenario for Office application,
especially in .NET as you can never rely on the Finalizer to release all outstanding COM
references.

http://support.microsoft.com/kb/257757

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top