Capture Excel Application in Matlab while opened on Desktop

1 次查看(过去 30 天)
Thre is an Excel Application already open on the Desktop. I want to capture it in MATLAB.
Line
exlFile = exlWkbk.Open(['C:/MATLAB7/work/path/file.xls']);
needs to be different.
exl = actxserver('excel.application');
exlWkbk = exl.Workbooks;
exlFile = exlWkbk.Open(['C:/MATLAB7/work/Relativity/CentrpetalAcceleration.xlsm']);
exlSheet1 = exlFile.Sheets.Item('Sheet1');
%code here
exlWkbk.Close;
exl.Quit;

回答(1 个)

Image Analyst
Image Analyst 2014-1-31
I think you want something like this
% Get the Excel COM server:
% Excel = actxGetRunningServer('Excel.Application')
Excel = actxserver('Excel.Application')
% Make it appear (it's invisible otherwise).
Excel.visible = true;
% Suppress Excel warning popups, like for overwriting a file.
Excel.DisplayAlerts = false;
% Open up the existing workbook named in the variable fullFileName.
excelFullFileName = 'C:\Users\Philosophaie\Documents\Spreadsheets\Expenses.xls'
if exist(excelFullFileName, 'file')
Excel.Workbooks.Open(excelFullFileName);
else
message = sprintf('Warning: %s is not there!', excelFullFileName);
uiwait(warndlg(message));
Excel.Close;
Excel.Quit;
clear('Excel') % Close our connection to Excel but don't shut it down. return;
end
% Code to do stuff with Excel.....
% Then finish up: save and close and delete object variable.
Excel.ActiveWorkbook.Save
Excel.ActiveWorkbook.Close
Excel.Quit
clear('Excel') % Close our connection to Excel but don't shut it down.
  1 个评论
Image Analyst
Image Analyst 2014-1-31
编辑:Image Analyst 2014-1-31
By the way, Quiting and clearing in the above code will shut down your workbook, but leave open any that were open before you started running this code. It doesn't shut down Excel for all workbooks, just those you opened in your MATLAB code.
Attached is a demo, in case anyone is interested, that does some more stuff like reading and writing via ActiveX. This is essential if you want to deal with multiple workbooks and don't want to wait forever for xlsread and xlswrite to work.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by