Re: lingering excel process
Reuben Sivan <[email protected]>
| Newsgroups | gmane.comp.windows.devel.jawin |
|---|---|
| Message-ID | <[email protected]> |
A few days ago I posted the same question.
The answer I received solved my problem.
For your convenience, I am re-posting the answer I got at the time:
"Hi, Excel is very sensitive with respect to left-over COM references.
Even if you quit and release the application-instance, if you have
unreleased objects like Range or Workbook or Worksheet, Excel will stay
alive. You must pay particular attention to objects returned from method
calls like for example Workbooks.Add which returns a Workbook that needs
to be released by you.
Stefan"
Your example should read something like the code below.
Hope it helps.
Reuben
public class testexcel
{
public static void main(String[] args) throws COMException
{
DispatchPtr app = null;
DispatchPtr wbooks = null;
DispatchPtr wbook = null;
try
{
Ole32.CoInitialize();
app = new DispatchPtr("Excel.Application");
app.put("Visible", false);
wbooks = (DispatchPtr) app.get("Workbooks");
wbook = (DispatchPtr) wbooks.invoke("Open", "c:\\t.xls");
wbook.invoke("Close");
wbooks.invoke("Close");
app.invoke("Quit");
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (wbook != null)
wbook.close();
if (wbooks != null)
wbooks.close();
if (app != null)
app.close();
Ole32.CoUninitialize();
}
}
}