Actxserver reading only first file

1 回表示 (過去 30 日間)
Ghenji
Ghenji 2018 年 2 月 15 日
コメント済み: Ghenji 2018 年 2 月 20 日
working with this code-
get_files = cellstr(get(handles.listboxfiles,'String'));
len = length(get_files);
for i = 1 : len
excel = actxserver('Excel.Application');
workbook = excel.Workbooks.Open(get_files{i});
.....
.....
.....
.....
.....
.....
end
Am not getting any error but Actxserver reads only first file from list. Listboxfiles contains name of the files with pathname. Something like this "C:\Users\ERTW\Documents\Excel_file\ARAN1.xlsm". Is something missing with excel.Workbooks.Open?
  1 件のコメント
Guillaume
Guillaume 2018 年 2 月 15 日
編集済み: Guillaume 2018 年 2 月 15 日
Note that your question has nothing to do with actxserver. actxserver is used to connect matlab to any COM/ActiveX component. It can be excel but it can be anything else such as other Office programs (Word, Access, Outlook, etc.), matlab itself, or anything that support scripting.
actxserver does not read files.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2018 年 2 月 15 日
Have you verified that get_files actually contains more than one element. Is your listbox multiselect?
There is nothing wrong with your Workbooks.Open call and you'd get an error if excel failed to open the workbook. Of course, you overwrite the workbook variable on each step of the loop, so if you expected an array of workbooks after the loop is finished, you're not going to get one.
Note that your code starts a new instance of excel at each step of the loop. That's very inefficient. You would be better off with:
excel = actxserver('Excel.Application'); %start excel
%excel.Visible = true; %uncomment for debugging
for fidx = 1:numel(get_files)
workbook = excel.Workbooks.Open(get_files{i});
%...
workbook.Close();
end
excel.Quit(); %Don't forget to quit each excel instance or they'll be left running in the background invisible
The best way for you to figure out what is going on is to make excel visible and see how your code affects the excel window.
  8 件のコメント
Guillaume
Guillaume 2018 年 2 月 20 日
編集済み: Guillaume 2018 年 2 月 20 日
Oh, yes sorry, it needs to be a vertcat instead of horzcat (aka []) since it's a cell array of column vectors
wanted_sheets = unique(vertcat(wanted_sheets{:}));
Ghenji
Ghenji 2018 年 2 月 20 日
And that's what was missing. Thanks lot Guillaume once again.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by