Matlab Actxserver :How could I terminate process opened by actxserver in matlab

后端 未结 1 824
走了就别回头了
走了就别回头了 2021-01-07 10:40

I want to open and close an Excel file in MATLAB. I have tried the code below, but it failed on closing process with actxserver

h.WorkBooks.Item(wbkname).Clo         


        
相关标签:
1条回答
  • 2021-01-07 11:20

    Here is an example that creates a new spreadsheet, write some values, save the file and exit. The Excel process is cleanly terminated at the end.

    % create Excel COM server
    excel = actxserver('Excel.Application');
    excel.Visible = true;  % make the window visible
    
    % create new workbook
    wb = excel.Workbooks.Add();
    
    % get "Sheet1" and activate it
    sheet = wb.Sheets.Item(1);
    sheet.Activate();
    
    % select a 5x5 range, and fill it with some numeric values
    sheet.Range('A1:E5').Value = num2cell(magic(5));
    
    % save spreadsheet file
    excel.DisplayAlerts = false;  % overwrite file without prompts
    wb.SaveAs(fullfile(pwd(),'myfile.xlsx'));
    
    % close spreadsheet
    wb.Close(false);
    
    % quit Excel
    excel.Quit();
    
    % delete handles and clear variables
    delete(excel);
    clear sheet wb excel
    

    You might additionally want to set certain properties appropriately, if you want the automation to be performed in the background with no user interaction:

    excel.Visible = false;         % invisible Excel window
    excel.ScreenUpdating = false;  % turn off screen update to run faster 
    excel.Interactive = false;     % non-interactive mode, with no keyboard/mouse
    excel.DisplayAlerts = false;   % no prompts or alert messages
    excel.UserControl = false;     % object freed when reference count reaches zero
    
    0 讨论(0)
提交回复
热议问题