473,388 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How to properly close Excel when finished?

Hi, I've written an application to do a while bunch of Excel automation.
I open a file, scan through all the worksheets, extract data, and then I
try to close Excel. However, I can see that I'm not doing it
effectively. If I am debugging my app, and I kill the app before my app
exits, but after the call that should close excel, I still have an
excell process sitting around. After about 20 + of those stack up, you
start to feel it.
If I quit all the way out of my app, somehow Excel DOES get cleaned up
and the instance / process is closed.

So, how do I properly close an instance of excel?

Thank you!
Currently doing it this way:

public void ExtractDataFromExcel(string fileName)
{

// Instantiate an Excel control object
ExcelObj = new Excel.Application();

// Disable alert messages
ExcelObj.DisplayAlerts = false;

// See if the Excel Application Object was successfully constructed
if (ExcelObj == null)
{
MessageBox.Show("ERROR: EXCEL couldn't be started!");
System.Windows.Forms.Application.Exit();
}

theWorkbook = ExcelObj.Workbooks.Open(
fileName, // Filename
0, // Update links
true, // Read only
5, // Format
"", // Password
"", // Write res password
true, // Ignore read-only recommend
Excel.XlPlatform.xlWindows, // Origin
"\t", // Delimiter (\t = tab delimited)
false, // Editable
false, // Notify
0, // Converter
false, // AddToMru
0, // Local
true // Corrupt load
);

// get the collection of sheets in the workbook
sheets = theWorkbook.Worksheets;
{ ... do some work ... }

// Close Excel workbook
theWorkbook.Close(false, fileName, 0);

object saveChanges = Missing.Value;
object originalFormat = Missing.Value;
object routeDocument = Missing.Value;

ExcelObj.Quit(); // ref saveChanges, ref originalFormat, ref
routeDocument);
} // end of method
Meet people for friendship, contacts,
or romance using free instant messaging software! See a picture you
like? Click once for a private conversation with that person!

www.SOCIALNETWORK.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
3 8354
Ah yes... I rembmer thins one... ran into it a couple years ago.

MS was kind enough to come up with a fix for me :)

http://support.microsoft.com/default...b;en-us;266088

Turns out the garbage collection on excel wasn't working right.
Hope that helps!

"David Berman" <da**********@bose.com> wrote in message
news:u$**************@TK2MSFTNGP11.phx.gbl...
Hi, I've written an application to do a while bunch of Excel automation.
I open a file, scan through all the worksheets, extract data, and then I
try to close Excel. However, I can see that I'm not doing it
effectively. If I am debugging my app, and I kill the app before my app
exits, but after the call that should close excel, I still have an
excell process sitting around. After about 20 + of those stack up, you
start to feel it.
If I quit all the way out of my app, somehow Excel DOES get cleaned up
and the instance / process is closed.

So, how do I properly close an instance of excel?

Thank you!
Currently doing it this way:

public void ExtractDataFromExcel(string fileName)
{

// Instantiate an Excel control object
ExcelObj = new Excel.Application();

// Disable alert messages
ExcelObj.DisplayAlerts = false;

// See if the Excel Application Object was successfully constructed
if (ExcelObj == null)
{
MessageBox.Show("ERROR: EXCEL couldn't be started!");
System.Windows.Forms.Application.Exit();
}

theWorkbook = ExcelObj.Workbooks.Open(
fileName, // Filename
0, // Update links
true, // Read only
5, // Format
"", // Password
"", // Write res password
true, // Ignore read-only recommend
Excel.XlPlatform.xlWindows, // Origin
"\t", // Delimiter (\t = tab delimited)
false, // Editable
false, // Notify
0, // Converter
false, // AddToMru
0, // Local
true // Corrupt load
);

// get the collection of sheets in the workbook
sheets = theWorkbook.Worksheets;
{ ... do some work ... }

// Close Excel workbook
theWorkbook.Close(false, fileName, 0);

object saveChanges = Missing.Value;
object originalFormat = Missing.Value;
object routeDocument = Missing.Value;

ExcelObj.Quit(); // ref saveChanges, ref originalFormat, ref
routeDocument);
} // end of method
Meet people for friendship, contacts,
or romance using free instant messaging software! See a picture you
like? Click once for a private conversation with that person!

www.SOCIALNETWORK.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #2
Thanks for the link, but it's specifically for a Java solution. Does
anyone have an equivalent for C#?

David


Meet people for friendship, contacts,
or romance using free instant messaging software! See a picture you
like? Click once for a private conversation with that person!

www.SEN.us

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
David,
Make sure you call Marshal.ReleaseComObject on each Excel object you
reference in your code and set them all to null/Nothing afterwards.

Ron Allen
"David Berman" <da**********@bose.com> wrote in message
news:e2**************@TK2MSFTNGP10.phx.gbl...
Thanks for the link, but it's specifically for a Java solution. Does
anyone have an equivalent for C#?

David


Meet people for friendship, contacts,
or romance using free instant messaging software! See a picture you
like? Click once for a private conversation with that person!

www.SEN.us

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Lorenzo Melato | last post by:
Hi everyone, I have a very strange problem. If I open a .NET application from EXCEL using SHELL function, when I close EXCEL, the EXCEL.EXE process remain active and I must close it by Task...
0
by: Stewart Allen | last post by:
Hi there, I've got some code to open up an Excel workbook but what I want is for the code to pause until the Excel workbook is closed. Sub EditWorkbook(strFile as String) Dim xlApp As...
5
by: Wenke Ji | last post by:
Hi I open a Excel workbook using below API: Set ExcelServer = CreateObject("EXCEL.Application") Set TargetWorkbook = ExcelServer.Workbooks.Open (CurrentBook) Befor the programm exit , I use...
35
by: Eric Sabine | last post by:
In my Finally block, I was using cn.close (where cn is an ADO.NET connection object, SQLConnection to be exact) and then I came across the following in some microsoft code. If Not cn Is Nothing...
6
by: icony | last post by:
Hi everyone, Here's my problem. I have a report that i want to use with the command openreport. The report open and close in a fraction of a second. I cant see why i cant view the thing. Cause i...
13
by: chuckie_9497 | last post by:
hello all you gurus. I am struggling with releasing com objects. I have isolated the problem to the code below. Objects are released and the process ends until I use "int k = sheet.Count;" Then...
4
by: Yoduh | last post by:
Hello, I'm fairly new to javascript. I'm making a website for my personal use that will read from Excel and display some data on an HTML page and I've been learning javascript as I go. A problem...
1
by: JDeats | last post by:
So I have a reference to Microsoft Office 10.0 Object Library in my WinForms project (VS.NET 2005, 2.0 Framework) using Microsoft.Office.Core; using Excel; I have an instance of...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.