xlswrite. find out if the destination file is open or close.

9 次查看(过去 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?

采纳的回答

Image Analyst
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 个评论
Milad
Milad 2014-1-29
编辑:Milad 2014-1-29
Thanks Image Analyst. I always learn new things from your codes. Actually there was another answer to this question and I tried that and it worked. I dont know why the other answer is not here anymore. Someone suggested using fopen. and I did it as below.
fid=fopen(Excelfile,'a');
% fclose(fid)
fidcc=0;
anss='a';
while fid==-1 && fidcc<5 && strcmp(anss, 'Continue. I dont neet results in Excel')==0
txtt='Warning: if the input Excel file is open, close it now. Then press: Ok. (While the file is open, the results can not be exported to Excel.)';
anss=questdlg(txtt,'Warning','Ok, It is closed now','Continue. I dont neet results in Excel','Ok, It is closed now');
switch anss
case 'Ok, It is closed now'
fid=fopen(Excelfile,'a');
case 'Continue. I dont neet results in Excel'
msgbox('Simulation is finished but results are not exported to Excel.')
end
end
fid=fopen(Excelfile,'a');
fclose('all');
if fid~=-1
xlswrite(Excelfile, output_table,'result','M15');
msgbox('Simulation is finished. See results in Excel.','Information')
end
The While loop was actually not necessary. That is for user's convenience.
Now, which one is faster or more reliable? ActiveX method or fopen?
I liked your find_running_processes.m and i'll keep a copy with me. I'm sure I will need that :).
Image Analyst
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 个)

Community Treasure Hunt

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

Start Hunting!

Translated by