xlswrite. find out if the destination file is open or close.
10 次查看(过去 30 天)
显示 更早的评论
Guys, I want to know if it is possible to check if an excel file is closed in Windows, so that I can write to it, or it is open. when it is open you will get the following error
Error using xlswrite (line 220)
The file C:\...\1.xlsm is not writable.
It may be locked by another process.
I want to check if the file is open or close (outside Matlab), before I use xlswrite. Mayb if I can ckeck if Excel process is ON in Windows Task Manager, that will help too.
So any idea?
0 个评论
采纳的回答
Image Analyst
2014-1-28
You can find out if the file is already open in Excel via ActiveX.
try
% Launch Excel.
Excel = actxserver('Excel.Application');
% Try to open a workbook.
if exist(excelFullFileName, 'file')
% Open up the existing workbook named in the variable fullFileName.
Excel.Workbooks.Open(excelFullFileName);
else
message = sprintf('File does not exist:\n%s', excelFullFileName);
end
catch ME
% You get here is the file is already open in Excel.
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s\nDo you already have the file %s open in Excel?', ...
ME.stack(1).name, ME.stack(1).line, ME.message, excelFullFileName);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
But here's a more general purpose chunk of code I wrote to determine if ANY process in Windows is running, and optionally to pause and wait for it to finish. Save the code below as "find_running_process.m" and run it.
% find_running_process.m
% Finds out if a process is running.
% Let's you monitor the process until it shuts down.
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Execute the system command
% tasklist /FI "IMAGENAME eq region_editor.exe"
% First define the name of the program we're looking for.
% You can run it then execute "tasklist" in a
% CMD console window if you don't know the exact name.
taskToLookFor = 'Excel.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))
if itIsRunning
message = sprintf('%s is running.', taskToLookFor);
uiwait(helpdlg(message));
else
message = sprintf('%s is not running.', taskToLookFor);
uiwait(helpdlg(message));
return; % Nothing else to do.
end
message = sprintf('Do you want to monitor it until it finishes?');
button = questdlg(message, 'Wait for shutdown?', 'Yes', 'No', 'Yes');
drawnow; % Refresh screen to get rid of dialog box remnants.
if strcmpi(button, 'No')
return;
end
% Go into a loop waiting for it to finish.
maxChecks = 10; % Max seconds to wait before exiting - a failsafe.
numberOfChecks = 1;
while itIsRunning && numberOfChecks < maxChecks
% 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));
if itIsRunning
message = sprintf('%s is still running after %d seconds.\n',...
taskToLookFor, numberOfChecks);
fprintf('%s', message);
else
message = sprintf('%s is not running anymore.\n', taskToLookFor);
fprintf('%s', message);
uiwait(helpdlg(message));
break; % Exit loop.
end
pause(1); % Wait a second before checking again.
numberOfChecks = numberOfChecks + 1;
end
msgbox('Done with demo!');
2 个评论
Image Analyst
2014-1-29
编辑:Image Analyst
2014-1-29
If you want to do something with the file in Excel, you're best off using ActiveX. If you just want to check on it for curiosity and not do anything with it, then using fopen() is simpler. Just remember to call fclose() if the fopen() succeeded or else you'll have just put a lock on it for no reason.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!