问题描述
我想在 MATLAB 中打开和关闭一个 Excel 文件.我已经尝试过 下面的代码,但它在使用
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).Close;
这是我解决此问题的代码,如何终止 excel 文件?
here is my code for this issue, how can I terminate the excel file?
.Quit
.delete
我也尝试通过 VBA 子模块关闭 excel 文件,但它给了我一个错误消息:
I also tried to close excel file via VBA sub-module, but it gives me an error message:
fullFileName = [pwd 'KOSPI200_1월.xlsm'];
excel = actxserver('Excel.Application');
file = excel.Workbooks.Open(fullFileName);
excel.Run('jongho_bot_initial');
excel.Run('jongho_bot_loop',2);
推荐答案
这是一个创建新电子表格、写入一些值、保存文件并退出的示例.Excel 进程最终完全终止.
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
这篇关于Matlab Actxserver:我如何终止 matlab 中 actxserver 打开的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!