gpt4 book ai didi

excel - matlab使用activex接口(interface)自动保存excel文件

转载 作者:太空宇宙 更新时间:2023-11-03 19:56:04 24 4
gpt4 key购买 nike

我在 matlab 中有一段代码。运行我的程序后,创建了一个文件“example2.xlsx”。

现在我有了下面的代码,我希望 matlab 将当前的“example2.xlsx”替换为新的“example2.xlsx”(自动保存而不询问我是否要替换它):

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

最佳答案

您可以将 Excel 应用程序对象的 DisplayAlerts 属性设置为 false 以停止显示这些对话框。

以下是您的代码的简化版本:

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

关于excel - matlab使用activex接口(interface)自动保存excel文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10134435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com