We can call a sheet using it's name or index by xlsread
function like this:
for i=1:100
file = xlsread(`filename.xlsx`,i)
%% I want get name of the called sheet here (storing in cell array)
end
In this loop if want call and extract the specified sheet name (using the index of sheet) as we can see in comment line of above loop and store the names in a cell array. How can I do this in MATLAB R2015a?
See xlsfinfo
, which has an optional sheets
output that returns a cell array of the sheet names. For example:
xlswrite('test.xlsx', 1:3, 'hi');
xlswrite('test.xlsx', 1:3, 'hello');
xlswrite('test.xlsx', 1:3, 'hey');
[~, sheets] = xlsfinfo('text.xlsx');
Returns:
sheets =
'Sheet1' 'hi' 'hello' 'hey'
Note that Sheet1
is only present because I generated a blank spreadsheet. If a sheet name specified in the xlswrite
call is not present in the target sheet it is added to the end of the workbook.
EDIT: ActiveX implementation:
eAX = actxserver('Excel.Application');
mywb = eAX.Workbooks.Open('C:\test.xlsx');
mysheets = eAX.sheets;
numsheets = mysheets.Count;
sheets = cell(1, numsheets);
for ii = 1:numsheets
sheets{ii} = eAX.Worksheets.Item(ii).Name;
end
mywb.Close(false)
eAX.Quit