杀死在Matlab中创建的Excel进程 [英] Kill Excel process created in Matlab

查看:72
本文介绍了杀死在Matlab中创建的Excel进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑到我写的是工作簿,当前我杀死了所有Excel进程,以便我的代码在循环调用时可以正常工作.

Given that I write to a workbook, I currently kill all Excel processes so that my code works when I call it in a loop.

xlswrite(path,values);
system('taskkill /F /IM EXCEL.EXE');

这使我在处理另一个Excel文件时无法运行代码.如何使Matlab仅终止自己创建的Excel进程?

This makes me unable to run the code while I am working in another Excel file. How do I make it so that Matlab terminates only the Excel processes that itself created?

推荐答案

这是R2015b周围引入的一个功能",可以加快对Excel的多次写入速度……对用户/内存的友好程度不是很高!

This was a "feature" introduced somewhere around R2015b to speed up multiple writes to Excel... not very user/memory friendly!

xlswrite 文档链接到此

The xlswrite documentation links to this MathWorks Support Answer to manually write to Excel using actxserver, you can then manually delete the COM object referencing Excel.

您实际上可以编辑xlswrite 并看到它使用了 matlab.io.internal.getExcelInstance ,它执行相同的操作并使用 actxserver创建一个COM接口.

You can actually edit xlswrite and see that it uses matlab.io.internal.getExcelInstance, which does the same thing and creates a COM interface with actxserver.

一个厚脸皮的选择是复制 xlswrite ,添加它创建的 Excel 变量作为输出,然后 Quit delete之后,如下所示.我不主张破坏MathWorks对该函数的任何版权所有权.

A cheeky option would be to copy xlswrite, add the Excel variable it creates as an output, and Quit and delete it afterwards, as shown below. I don't advocate breaking any of The MathWorks' copyright ownership of that function.

一个不太麻烦的选择是根据我上面链接的答案创建一个可比较的函数,仅用于写数据就看起来像这样:

A less cheeky option would be to create a comparable function based on the answer I linked above, for writing data only it would look something like this:

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

您基本上可以使用 COM 对象 h 自定义新 Excel 工作簿中的所有内容,因此您可以添加您在 xlswrite 中使用的任何功能,例如工作表命名等.

You can customise basically everything within the new Excel workbook using the COM object h, so you could add any functionality which you use in xlswrite like sheet naming etc.

这篇关于杀死在Matlab中创建的Excel进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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