MATLAB使用ActiveX调用Office

尽管MATLAB带有高级的Office交互接口(xlsread/xlswrite等),但是对于高级的应用仍不能满足,且慢的难以忍受。因此,使用MATLAB的ActiveX调用成了最佳配合

创建ActiveX对象

try
    h = actxGetRunningServer('Excel.Application');
    if(h.Visible) , error 'not actx'; end
catch
    try
        h = actxserver('Excel.Application');
        % config
        h.Visible = false;         % invisible Excel window
        h.ScreenUpdating = false;  % turn off screen update to run faster
        h.Interactive = false;     % non-interactive mode, with no keyboard/mouse
        h.DisplayAlerts = false;   % no prompts or alert messages
        %h.UserControl = false;     % object freed when reference count reaches zero
    catch
        h = [];
    end
end

操作工作表

r = h.Workbooks;
t = h.wb.Open(‘1.xlsx’);
t.Range('A1').Value = ‘hello’;
t.Range('A1').Borders.Color = 500;
t.Activate;
t.SaveAs(xlsf);
t.Close;

退出ActiveX对象

h.Quit
h.delete

更多函数使用说明

https://msdn.microsoft.com/zh-cn/vba/excel-vba/articles/object-model-excel-vba-reference

点赞