我想在MATLAB中打开和关闭Excel文件。 我在下面尝试了the code,但在使用actxserver
关闭流程时失败了h.WorkBooks.Item(wbkname).Close;
这是我的问题代码,如何终止excel文件?
.Quit
.delete
我还尝试通过VBA子模块关闭excel文件,但它给出了一条错误消息:
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);
答案 0 :(得分:2)
以下是创建新电子表格,编写一些值,保存文件并退出的示例。 Excel流程在最后完全终止。
% 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
如果您希望在后台执行自动化而无需用户交互,您可能还需要适当地设置certain properties:
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