How to stop Excel processes running in the background, which were started from MATLAB functions xlsread, xlswrite, xlsinfo.

90 ビュー (過去 30 日間)
After using xlsread, a(n) Excel process(es) stay(s) running in the background, even after the function finished executing.
How can I stop the processes from within MATLAB?

採用された回答

MathWorks Support Team
MathWorks Support Team 2024 年 1 月 18 日
編集済み: MathWorks Support Team 2024 年 2 月 26 日
In MATLAB R2015b and later, xlsread, xlswrite, xlsfinfo use one Excel process that is opened when one of these functions is opened for the first time and stays open until MATLAB closes.
These functions reuse this process for every call instead of opening and closing Excel every time. This greatly improves the performance of successive calls to xlsread, xlswrite, xlsfinfo.
Solution 1
As of R2019a, the recommendation is not to use xlsread, and instead is to use the readtable function. 
More information about how to transition xlsread code to readtable and similar functions is available here:
>> T = readtable('SampleExcel.xlsx','UseExcel',false);
If you are using a version that supports these functions, it is recommended you use these instead.
Solution 2
Started Excel processes persist stay open in the background, but closing MATLAB should kill the Excel process.
If you want to terminate Excel before closing MATLAB then the following example demonstrates how to close Excel processes started by MATLAB
% Shows a list of active excel processes
!tasklist /FI "IMAGENAME eq excel.exe"
% STOPS ALL EXCEL PROCESSES [USE WITH CAUTION]
system('taskkill /F /IM EXCEL.EXE');
% Starts an Excel Application
excelApp = actxserver('Excel.Application');
% Starts a second Excel Application
excelFile = xlswrite('testfile.xlsx',randn(5));
% Uses the second Excel Instance
xlsread('testfile.xlsx');
% Shows now two running Excel processes
!tasklist /FI "IMAGENAME eq excel.exe"
% Stops the second instance
clear getExcelInstance
!tasklist /FI "IMAGENAME eq excel.exe"
% Stops the first instance
Quit(excelApp)
delete(excelApp)
clear excelApp
% q.e.d.
!tasklist /FI "IMAGENAME eq excel.exe"
Solution 3
Using 'basic' as the fourth argument to xlsread will prevent it from opening Excel instances and use another method instead, but has performance limitations and cannot be used to select ranges of data.
>> [num,txt,raw] = xlsread('SampleExcel.xlsx', '', '', 'basic');
"Basic" flag descibed here:
Limitations described here:
Solution 4
If the Solutions above fail to close the Excel processes spawned by MATLAB or closes those not opened by MATLAB, this solution may apply. 
Such systems include a Windows workstation or server that runs Continuous Integration (CI) tests such as GitLab or another Git CI system.
It should be noted that this is a solution that involves compiling a MATLAB executable C file on the workstation that runs the Test Suite,
so it is not suitable for use in a customer-facing application built with MATLAB because most users do not have C compilers installed on their machines.
Depending on the workflow, calling the closeExcel function at the end of the test suite should be suitable, but it could also be called at the end of each test case if that is found to be more effective.
Example: 
The getProcessId.c file needs to be compiled into a MEX file by executing:
>> mex getProcessId.c
At the end of the Test Suite
>> closeExcel()
when closeExcel.m and getProcessId.mex___ are both on the MATLAB path.
The MEX file only needs to be compiled on the machine once, but if there are multiple workstations that run the test suite, it will have to be compiled on each separately.
It is a Windows-specific solution because on other systems MATLAB does not spawn Excel COM objects.
To select the C compiler you would like to use or to get a link to the support package that installs the MINGW C compiler, you can execute:
>> mex -setup
 

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

タグ

タグが未入力です。

製品


リリース

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by