matlab automatically save excel file using activex interface

前端 未结 1 1035
眼角桃花
眼角桃花 2021-01-13 12:05

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 \'exam

相关标签:
1条回答
  • 2021-01-13 12:41

    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
    
    0 讨论(0)
提交回复
热议问题