Are there any examples that show how to use the ActiveX automation interface to connect MATLAB to Excel?

166 vues (au cours des 30 derniers jours)
I am trying to control Excel from MATLAB using ActiveX. I would like some examples that show how to use the ActiveX automation interface from Excel to do this.

Réponse acceptée

MathWorks Support Team
MathWorks Support Team le 10 Déc 2021
Modifié(e) : MathWorks Support Team le 13 Déc 2021
This enhancement has been incorporated into the documentation for Release 14 Service Pack 3 (R14SP3). You can find an example in the "External Interfaces" manual in the chapter "COM and DDE Support (Windows Only)" under "Using MATLAB as an ActiveX Client" or on the web:
For previous product releases, please see the example below:
Most of the functionality that you get from ActiveX is dependent on the object model, which the external application implements. Consequently, we are usually unable to provide much information about the functions that you need to use in the remote application to perform a particular function. We do, however, have an example that shows how to perform common functions in Excel.
We also recommend that you become more familiar with the Excel object model in order to better use Excel's ActiveX automation interface from MATLAB. You can find more information on this interface by selecting the "Microsoft Excel Visual Basic Reference" topic in the Microsoft Excel Help Topic dialog. This topic area contains a searchable description of Excel methods and properties.
The following example demonstrates how to insert MATLAB data into Excel. It also shows how to extract some data from Excel into MATLAB. For more information, refer to the individual comments for each code segment.
% Open Excel, add workbook, change active worksheet,
% get/put array, save, and close
% First open an Excel Server
Excel = actxserver('Excel.Application');
set(Excel, 'Visible', 1);
% Insert a new workbook
Workbooks = Excel.Workbooks;
Workbook = invoke(Workbooks, 'Add');
% Make the second sheet active
Sheets = Excel.ActiveWorkBook.Sheets;
sheet2 = get(Sheets, 'Item', 2);
invoke(sheet2, 'Activate');
% Get a handle to the active sheet
Activesheet = Excel.Activesheet;
% Put a MATLAB array into Excel
A = [1 2; 3 4];
ActivesheetRange = get(Activesheet,'Range','A1:B2');
set(ActivesheetRange, 'Value', A);
% Get back a range. It will be a cell array,
% since the cell range can
% contain different types of data.
Range = get(Activesheet, 'Range', 'A1:B2');
B = Range.value;
% Convert to a double matrix. The cell array must contain only scalars.
B = reshape([B{:}], size(B));
% Now save the workbook
invoke(Workbook, 'SaveAs', 'myfile.xls');
% To avoid saving the workbook and being prompted to do so,
% uncomment the following code.
% Workbook.Saved = 1;
% invoke(Workbook, 'Close');
% Quit Excel
invoke(Excel, 'Quit');
% End process
delete(Excel);
There are several options for connecting MATLAB with Excel. For an example that shows how to connect MATLAB with Excel using Excel Link, refer to the "Related Solutions" below.
For an example that shows how to connect MATLAB with Excel using DDE, refer to the "Related Solutions" below.
For information on how to use the XLSREAD function to read .xls files, please refer to the following URL:
  3 commentaires
dpb
dpb le 26 Jan 2023
Just stumbled over this thread -- while it's old, the Q? is a common one for newbies -- the answer is, with ActiveX COM interface, you have to count and pass all arguments by position as shown in the MS Excel VBA or C# documentation; there is no way to use the named parameter value pair from MATLAB COM; it doesn't have the VBA or C# compiler available to make that substitution for you.
Pass empty "[]" arguments for all those preceding those you need as placeholders to get the right positional location for the argument of interest.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by