考虑到我写的是工作簿,当前我将杀死所有Excel进程,以便当我在循环中调用它时我的代码可以工作。
xlswrite(path,values);
system('taskkill /F /IM EXCEL.EXE');
Run Code Online (Sandbox Code Playgroud)
这使我无法在另一个Excel文件中运行代码。如何使Matlab仅终止自己创建的Excel进程?
这是R2015b周围某个地方引入的一个“功能”,可加快对Excel的多次写入速度……对用户/内存的友好程度不是很高!
该xlswrite文件链接到本MathWorks的支持答手动使用写到Excel actxserver,则可以手动删除COM对象引用的Excel。
您实际上可以edit xlswrite看到它使用matlab.io.internal.getExcelInstance,它做同样的事情,并使用创建一个COM接口actxserver。
面露选择是将复制xlswrite,添加Excel它创建作为输出变量,Quit以及delete它之后,如下所示。我不主张破坏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
Run Code Online (Sandbox Code Playgroud)
您基本上可以使用COM对象自定义新Excel工作簿中的所有内容h,因此可以添加在工作xlswrite表命名等中使用的任何功能。
| 归档时间: |
|
| 查看次数: |
243 次 |
| 最近记录: |