Hellow everyone . i am using
winopen('myfile.xlsx')
to open an excel file , after doing some modification, i want to close the excel file in order to overwrite the existing data by
xlswrite('myfile.xlsx', my_modified_data )
but sometimes i forgot to close the file manually in between and MATLAB generates an error . Is there any way to automatically close the excel file(by some command like winclose or something like that) before writing into it . Thank You all .

 채택된 답변

Image Analyst
Image Analyst 2014년 11월 21일

1 개 추천

Yes. You can use actxGetRunningServer() to connect to Excel and shut it down.

댓글 수: 9

pradeep kumar
pradeep kumar 2014년 11월 22일
@ Image Analyst , Thank yoy . Please explain me in detail or guide me some link . i am just a beginner to MATLAB
It's not hard:
try
% See if there is an existing instance of Excel running.
% If Excel is NOT running, this will throw an error and send us to the catch block below.
Excel = actxGetRunningServer('Excel.Application');
% If there was no error, then we were able to connect to it.
Excel.Quit; % Shut down Excel.
catch
% No instance of Excel is currently running.
end
pradeep kumar
pradeep kumar 2014년 11월 22일
@ Image Analyst . Thanks a lot Sir .
daniel
daniel 2016년 9월 27일
This actually doesn't kill the process. How do I kill the process??
KAE
KAE 2017년 7월 12일
Also if several Excel spreadsheets are open, this may not just close the one that was opened with winopen.
This will kill Excel:
system('taskkill /F /IM EXCEL.EXE');
Ihaveaquest
Ihaveaquest 2023년 3월 30일
would i be able to use PID to kill the curent open excel and not all ?
Ihaveaquest
Ihaveaquest 2023년 3월 30일
works for powerpoint as well - theres not much info online for how to with powerpoint
import mlreportgen.ppt.*
taskToLookFor = 'Powerpnt.exe';
% Now make up the command line with the proper argument
% that will find only the process we are looking for.
commandLine = sprintf('tasklist /FI "IMAGENAME eq %s"', taskToLookFor)
% Now execute that command line and accept the result into "result".
[status result] = system(commandLine)
% Look for our program's name in the result variable.
itIsRunning = strfind(lower(result), lower(taskToLookFor))
%closes powerpoints if they are open so system does not crasah when it
%tries to open a new PP
if itIsRunning
if exist('Plotted Circulator', 'file')==0
system('taskkill /F /IM POWERPNT.EXE');
end
else
end
@Ihaveaquest if Excel is open with several sheets, you can use
Excel = actxGetRunningServer('Excel.Application');
to get a handle to the Excel server, then use Excel to save the active sheet and close it.
Excel.ActiveWorkbook.Save;
Excel.ActiveWorkbook.Close(false); % The 'false' argument prevents the popup window from showing, forcing the closure without user intervention.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Artem Smirnov
Artem Smirnov 2017년 1월 19일

0 개 추천

Try this:
close all

댓글 수: 1

Image Analyst
Image Analyst 2017년 1월 19일
And when/if you do, you'll see that close does not shutdown other processes and applications like Excel.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

질문:

2014년 11월 21일

댓글:

2023년 3월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by