By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

Excel automation memory leak

P: n/a
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...b;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!

Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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" <ro*************@uswitch.nospam.com> wrote in message
news:A2**********************************@microsof t.com...
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...b;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!

Nov 18 '05 #2

P: n/a
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" <ro*************@uswitch.nospam.com> wrote in message
news:A2**********************************@microsof t.com...
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...b;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!

Nov 18 '05 #3

P: n/a
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.
Nov 18 '05 #4

P: n/a
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.

Nov 18 '05 #5

P: n/a
This article covers the subject in depth:
http://www.aspnetpro.com/NewsletterA...200309so_l.asp

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://Steve.Orr.net
"RoboSchro" <ro*************@uswitch.nospam.com> wrote in message
news:A2**********************************@microsof t.com...
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...b;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!

Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.