Excel automation memory leak


G

Guest

I'm building a reporting system in asp.net which needs an "export to Excel" function. We have Excel 2000 in the office. So I've added a reference to the Microsoft Excel 9.0 Object Library.

Having added "<identity impersonate='true'/>" to my Web.config, I can now create an instance of Excel and manipulate it, more or less as described in KB#306023 ( http://support.microsoft.com/default.aspx?scid=kb;EN-US;306023 ).

However, each time I do this, I end up with another EXCEL.EXE process staying active. I can't even manually end them (I get an "Unable to terminate process" "Access denied" error).

The core lines look like this:
Excel.Application xl = new Excel.Application();
Excel.Workbooks xwbs = xl.Workbooks;
Excel.Workbook xwb = xwbs.Add( Missing.Value );
Excel.Sheets xwss = xwb.Worksheets;
Excel.Worksheet xws = (Excel.Worksheet)xwss.get_Item( 1 );
Excel.Range xr1 = xws.get_Range( "A1", Missing.Value );
xr1.Value = "Hello world!";
xwb.SaveAs( @"C:\helloworld.xls", Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value );
xwb.Close( false, Missing.Value, Missing.Value );
xl.Quit();

The Quit() method doesn't seem to have any effect.

What do I need to do to stop these processes?

Any help will be greatly appreciated!
 
Ad

Advertisements

M

Morgan

This is a bad idea. Excel is not thread-safe for use in ASP.Net
applications.
I would recommend sending the data down via .csv or some other format that
Excel can natively open & read.
If you need .xls format, I recommend takinig a look at ComponentOne's XLS
component.

The reason ( I think) you can't terminate the process, is because you're not
setting the instance of Excel to nothing when you are done.
xl = nothing;


--Morgan

RoboSchro said:
I'm building a reporting system in asp.net which needs an "export to
Excel" function. We have Excel 2000 in the office. So I've added a reference
to the Microsoft Excel 9.0 Object Library.
Having added "<identity impersonate='true'/>" to my Web.config, I can now
create an instance of Excel and manipulate it, more or less as described in
KB#306023 (
http://support.microsoft.com/default.aspx?scid=kb;EN-US;306023 ).
However, each time I do this, I end up with another EXCEL.EXE process
staying active. I can't even manually end them (I get an "Unable to
terminate process" "Access denied" error).
The core lines look like this:
Excel.Application xl = new Excel.Application();
Excel.Workbooks xwbs = xl.Workbooks;
Excel.Workbook xwb = xwbs.Add( Missing.Value );
Excel.Sheets xwss = xwb.Worksheets;
Excel.Worksheet xws = (Excel.Worksheet)xwss.get_Item( 1 );
Excel.Range xr1 = xws.get_Range( "A1", Missing.Value );
xr1.Value = "Hello world!";
xwb.SaveAs( @"C:\helloworld.xls", Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value );
 
M

M. Zeeshan Mustafa

Microsoft says that office automation from a non-interactive
environment (asp/services) is not recommended.

Microsoft recommends to use some third party components
for writing excel/word..

check this reference link:
http://support.microsoft.com/?id=257757

--
Hope this helps,
Zeeshan Mustafa, MCSD


RoboSchro said:
I'm building a reporting system in asp.net which needs an "export to
Excel" function. We have Excel 2000 in the office. So I've added a reference
to the Microsoft Excel 9.0 Object Library.
Having added "<identity impersonate='true'/>" to my Web.config, I can now
create an instance of Excel and manipulate it, more or less as described in
KB#306023 (
http://support.microsoft.com/default.aspx?scid=kb;EN-US;306023 ).
However, each time I do this, I end up with another EXCEL.EXE process
staying active. I can't even manually end them (I get an "Unable to
terminate process" "Access denied" error).
The core lines look like this:
Excel.Application xl = new Excel.Application();
Excel.Workbooks xwbs = xl.Workbooks;
Excel.Workbook xwb = xwbs.Add( Missing.Value );
Excel.Sheets xwss = xwb.Worksheets;
Excel.Worksheet xws = (Excel.Worksheet)xwss.get_Item( 1 );
Excel.Range xr1 = xws.get_Range( "A1", Missing.Value );
xr1.Value = "Hello world!";
xwb.SaveAs( @"C:\helloworld.xls", Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value );
 
G

Guest

Thanks!

Interesting that this approach in general is considered a bad idea. The first time I got the idea was from another KB article (311194) where Microsoft tell you to do exactly that -- automate an Excel document from an asp.net page.

Ah well.
 
G

Guest

Thanks.

It does sound like this is a bad idea in general.

Setting the object to Nothing isn't the answer, though. I'm writing in C#, and there's no such statement. You can usually forcibly dispose of objects, but the Excel.Application doesn't allow this.
 
Ad

Advertisements

S

Steve C. Orr [MVP, MCSD]

This article covers the subject in depth:
http://www.aspnetpro.com/NewsletterArticle/2003/09/asp200309so_l/asp200309so_l.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net


RoboSchro said:
I'm building a reporting system in asp.net which needs an "export to
Excel" function. We have Excel 2000 in the office. So I've added a reference
to the Microsoft Excel 9.0 Object Library.
Having added "<identity impersonate='true'/>" to my Web.config, I can now
create an instance of Excel and manipulate it, more or less as described in
KB#306023 (
http://support.microsoft.com/default.aspx?scid=kb;EN-US;306023 ).
However, each time I do this, I end up with another EXCEL.EXE process
staying active. I can't even manually end them (I get an "Unable to
terminate process" "Access denied" error).
The core lines look like this:
Excel.Application xl = new Excel.Application();
Excel.Workbooks xwbs = xl.Workbooks;
Excel.Workbook xwb = xwbs.Add( Missing.Value );
Excel.Sheets xwss = xwb.Worksheets;
Excel.Worksheet xws = (Excel.Worksheet)xwss.get_Item( 1 );
Excel.Range xr1 = xws.get_Range( "A1", Missing.Value );
xr1.Value = "Hello world!";
xwb.SaveAs( @"C:\helloworld.xls", Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value );
 
Ad

Advertisements


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

Similar Threads


Top