xlswrite. find out if the destination file is open or close.
5 ビュー (過去 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!