Home » excel » php – How to force Excel process to quit after COM automation?

php – How to force Excel process to quit after COM automation?

Posted by: admin May 14, 2020 Leave a comment


In PHP I create an XLS-file using COM-object.

$excel = new COM("Excel.Application") or die("Excel is not installed!");
$excel = null;

After this code has been run, Excel.exe process still be in memory for ~ 6 minutes. How to force this process to quit immediately?

How to&Answers:

You are using COM objects, so you must be talking about php on Windows. Basically, you can kill tasks using the command line tool taskkill. You can call that using the php command shell_exec, like

shell_exec('taskkill /F /IM "excel.exe"');

If you don’t have the rights (but I believe you should because you started this process), run the cmd line using runas.

BUT! You are using php, so if you are running this in a web environment and people are accessing your website simultaneously, you might have multiple instances of Excel running. If that is the case, you’d have to figure out which one you want to kill.


The process sticks around for some time in case another “Excel.Application” object is requested, in which case we can avoid the cost of starting a new process and loading all the DLLs.

I’d just see it as a feature.


There is another way…
Try Uninitializing COMS. Instead of doing it just once, do it 4 times. This was established through trial and error so I do not have a valid reason for it except that it works. However, this does not work all the time. It might just work in your case