matlab automatically save excel file using activex interface

你离开我真会死。 提交于 2019-12-01 05:27:26

问题


I have a code in matlab. After I have run my program, a file 'example2.xlsx' was created.

Now I have the code below and I want matlab to replace the current 'example2.xlsx' by the new 'example2.xlsx' (saving automatically without asking me if I want to replace it):

e = actxserver ('Excel.Application'); % # open Activex server
filename = fullfile(pwd,'example2.xlsx'); % # full path required
ewb = e.Workbooks.Open(filename); % # open the file
esh = ewb.ActiveSheet;


str = num2str(num_rows+1);
esh.Range(strcat('J',str)).Interior.Color = clr;

sheet1 = e.Worksheets.get('Item', 'Sheet1');
range1 = get(sheet1,'Range', strcat('A',str),strcat('I',str));
range1.Value = values{num_rows+1};

[num, txt, raw] = xlsread('example2.xlsx');
num_rows = length(num(:,1));


xlWorkbookDefault = 51; % # it's the Excel constant, not sure how to pass it other way
ewb.SaveAs(fullfile(pwd,'example2'), xlWorkbookDefault)
ewb.Close(false)
e.Quit
e.delete

回答1:


You can set the DisplayAlerts property of the Excel application object to false to stop these dialogs from appearing.

The following is a simplified version of your code:

e = actxserver ('Excel.Application'); % # open Activex server
filename = fullfile(pwd,'example2.xlsx'); % # full path required
ewb = e.Workbooks.Open(filename); % # open the file
esh = ewb.ActiveSheet;

sheet1 = e.Worksheets.get('Item', 'Sheet1');
range1 = get(sheet1,'Range', 'A1');
range1.Value = 3;

set(e, 'DisplayAlerts', 0); % # Stop dialog!

xlWorkbookDefault = 51; % # it's the Excel constant, not sure how to pass it other way
ewb.SaveAs(fullfile(pwd,'example2'), xlWorkbookDefault)
ewb.Close(false)
e.Quit
e.delete


来源:https://stackoverflow.com/questions/10134435/matlab-automatically-save-excel-file-using-activex-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!