matlab使用activex界面自动保存excel文件 [英] matlab automatically save excel file using activex interface

查看:641
本文介绍了matlab使用activex界面自动保存excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



现在我有下面的代码,我想让matlab替换当前的'example2.xlsx '通过新的'example2.xlsx'(自动保存,不问我是否要替换它):

  e = actxserver ( 'Excel.Application'); %#打开ActiveX服务器
filename = fullfile(pwd,'example2.xlsx'); %#全路径需要
ewb = e.Workbooks.Open(filename); %#打开文件
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; %#这是Excel常量,不知道如何传递其他方式
ewb.SaveAs(fullfile(pwd,'example2'),xlWorkbookDefault)
ewb.Close(false)
e。退出
e.delete


解决方案

您可以设置Excel应用程序对象的DisplayAlerts属性为false,以阻止这些对话框出现。



以下是代码的简化版本:

  e = actxserver('Excel.Application'); %#打开ActiveX服务器
filename = fullfile(pwd,'example2.xlsx'); %#全路径需要
ewb = e.Workbooks.Open(filename); %#打开文件
esh = ewb.ActiveSheet;

sheet1 = e.Worksheets.get('Item','Sheet1');
range1 = get(sheet1,'Range','A1');
range1.Value = 3;

set(e,'DisplayAlerts',0); %#停止对话框!

xlWorkbookDefault = 51; %#这是Excel常量,不知道如何传递其他方式
ewb.SaveAs(fullfile(pwd,'example2'),xlWorkbookDefault)
ewb.Close(false)
e。退出
e.delete


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 'example2.xlsx' by the new 'example2.xlsx' (saving automatically without asking me if I want to replace it):

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

解决方案

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

这篇关于matlab使用activex界面自动保存excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆