在Matlab中创建Excel程序
考虑到我对工作簿的写入,我目前会终止所有Excel进程,这样当我在循环中调用它时,我的代码就能正常工作在Matlab中创建Excel程序,matlab,Matlab,考虑到我对工作簿的写入,我目前会终止所有Excel进程,这样当我在循环中调用它时,我的代码就能正常工作 xlswrite(path,values); system('taskkill /F /IM EXCEL.EXE'); 这使我无法在另一个Excel文件中运行代码。如何使Matlab只终止自己创建的Excel进程?这是R2015b中引入的一个“功能”,用于加速对Excel的多次写入。。。用户/内存不太友好 xlswrite文档链接到此链接,使用actxserver手动写入Excel,然后可以
xlswrite(path,values);
system('taskkill /F /IM EXCEL.EXE');
这使我无法在另一个Excel文件中运行代码。如何使Matlab只终止自己创建的Excel进程?这是R2015b中引入的一个“功能”,用于加速对Excel的多次写入。。。用户/内存不太友好
xlswrite
文档链接到此链接,使用actxserver
手动写入Excel,然后可以手动删除引用Excel的COM对象
实际上,您可以编辑xlswrite
,并看到它使用matlab.io.internal.getExcelInstance
,这与actxserver
做同样的事情并创建一个COM接口
一个厚颜无耻的选择是复制xlswrite
,添加它创建的Excel
变量作为输出,然后退出并删除它,如下所示。我不主张破坏MathWorks对该函数的版权所有权
一个不那么厚颜无耻的选择是基于我上面链接的答案创建一个可比较的函数,对于只写数据,它看起来像这样:
function xlswriteClean( File, Data, Range )
% XLSWRITECELAN writes data like XLSWRITE, but deletes the Excel instance!
% XLSWRITECELAN (FILE,DATA,RANGE) writes the variable in
% DATA to FILE, in the range specified by RANGE.
% RANGE is optional, defaults to "A1"
% Obtain the full path name of the file
% Could handle this more elegantly, i.e.
% this always assumes the current directory, but user might give a full path
file = fullfile(pwd, File);
% Open an ActiveX connection to Excel
h = actxserver('excel.application');
%Create a new work book (excel file)
wb = h.WorkBooks.Add();
% Select the appropriate range
if nargin < 3
Range = 'A1';
end
rng = h.Activesheet.get('Range', Range);
% Write the data to the range
rng.value = Data;
% Save the file with the given file name, close Excel
wb.SaveAs( File );
% Clean up - the point of this function
wb.Close;
h.Quit;
h.delete;
end
函数xlswriteClean(文件、数据、范围)
%XLSWriteClan像XLSWRITE一样写入数据,但删除Excel实例!
%XLSWriteClan(文件、数据、范围)将变量写入
%要文件的数据,在范围指定的范围内。
%范围是可选的,默认为“A1”
%获取文件的完整路径名
%可以更优雅地处理这个问题,即。
%这始终假定当前目录,但用户可能给出完整路径
文件=完整文件(pwd,文件);
%打开与Excel的ActiveX连接
h=actxserver('excel.application');
%创建新工作簿(excel文件)
wb=h.WorkBooks.Add();
%选择适当的范围
如果nargin<3
范围='A1';
结束
rng=h.Activesheet.get('Range',Range);
%将数据写入范围
rng.value=数据;
%使用给定的文件名保存文件,关闭Excel
wb.SaveAs(文件);
%清理-此功能的要点
wb.关闭;
h、 退出;
h、 删除;
结束
您可以使用COM对象h
自定义新Excel工作簿中的所有内容,因此您可以添加xlswrite
中使用的任何功能,如工作表命名等。这是R2015b附近引入的一个“功能”,用于加速对Excel的多次写入。。。用户/内存不太友好
xlswrite
文档链接到此链接,使用actxserver
手动写入Excel,然后可以手动删除引用Excel的COM对象
实际上,您可以编辑xlswrite
,并看到它使用matlab.io.internal.getExcelInstance
,这与actxserver
做同样的事情并创建一个COM接口
一个厚颜无耻的选择是复制xlswrite
,添加它创建的Excel
变量作为输出,然后退出并删除它,如下所示。我不主张破坏MathWorks对该函数的版权所有权
一个不那么厚颜无耻的选择是基于我上面链接的答案创建一个可比较的函数,对于只写数据,它看起来像这样:
function xlswriteClean( File, Data, Range )
% XLSWRITECELAN writes data like XLSWRITE, but deletes the Excel instance!
% XLSWRITECELAN (FILE,DATA,RANGE) writes the variable in
% DATA to FILE, in the range specified by RANGE.
% RANGE is optional, defaults to "A1"
% Obtain the full path name of the file
% Could handle this more elegantly, i.e.
% this always assumes the current directory, but user might give a full path
file = fullfile(pwd, File);
% Open an ActiveX connection to Excel
h = actxserver('excel.application');
%Create a new work book (excel file)
wb = h.WorkBooks.Add();
% Select the appropriate range
if nargin < 3
Range = 'A1';
end
rng = h.Activesheet.get('Range', Range);
% Write the data to the range
rng.value = Data;
% Save the file with the given file name, close Excel
wb.SaveAs( File );
% Clean up - the point of this function
wb.Close;
h.Quit;
h.delete;
end
函数xlswriteClean(文件、数据、范围)
%XLSWriteClan像XLSWRITE一样写入数据,但删除Excel实例!
%XLSWriteClan(文件、数据、范围)将变量写入
%要文件的数据,在范围指定的范围内。
%范围是可选的,默认为“A1”
%获取文件的完整路径名
%可以更优雅地处理这个问题,即。
%这始终假定当前目录,但用户可能给出完整路径
文件=完整文件(pwd,文件);
%打开与Excel的ActiveX连接
h=actxserver('excel.application');
%创建新工作簿(excel文件)
wb=h.WorkBooks.Add();
%选择适当的范围
如果nargin<3
范围='A1';
结束
rng=h.Activesheet.get('Range',Range);
%将数据写入范围
rng.value=数据;
%使用给定的文件名保存文件,关闭Excel
wb.SaveAs(文件);
%清理-此功能的要点
wb.关闭;
h、 退出;
h、 删除;
结束
您可以使用COM对象h
自定义新Excel工作簿中的所有内容,因此您可以添加在xlswrite
中使用的任何功能,如工作表命名等。您可以通过powershell创建Excel进程并获取其进程id,然后使用进程id终止该进程:
[~, pid] = system('powershell (Start-Process excel.exe -passthru).Id');
% Do your work
% ...
system(['powershell Stop-Process -Id ' pid])
您可以通过powershell访问excel进程并获取其进程id,然后使用该进程id终止该进程:
[~, pid] = system('powershell (Start-Process excel.exe -passthru).Id');
% Do your work
% ...
system(['powershell Stop-Process -Id ' pid])
从R2019a起,建议使用或函数写入excel文件。@rahnema1这些函数没有相同的问题吗?我已经使用类似名称的writetable
很久了,它认为如果写入xlsx,会打开隐藏的Excel实例…@Wolfie这些函数有一个选项“UseExcel”,应该设置为false
。从R2019a开始,或者是建议写入Excel文件的函数。@rahnema1这些函数没有同样的问题?我已经使用类似名称的writetable
很长时间了,它认为如果写入xlsx,会打开一个隐藏的Excel实例…@Wolfie这些函数有一个选项“UseExcel”,应该设置为false
。