Count number of sheets in excel file
86 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I want to count the number of sheets in an excel file. I want to read from an excel file and count the number of sheets in that file and save that number to use as iteration count in a for loop.
0 commentaires
Réponses (3)
laurent jalabert
le 4 Fév 2022
Déplacé(e) : Image Analyst
le 6 Fév 2023
d = uigetdir(pwd, 'Select a folder');
DATA = dir(fullfile(d, '*.xlsx'));
[status,sheets] = xlsfinfo(DATA.name);
sheets = sheetnames(DATA.name);
length(sheets)
0 commentaires
Sulaymon Eshkabilov
le 5 Fév 2023
Modifié(e) : Sulaymon Eshkabilov
le 5 Fév 2023
It is recommended to use: sheetnames() instead of xlsfinfo(), e.g.:
clearvars
D = uigetdir(pwd, 'Choose a folder to import XLSX data');
XLSfile = dir(fullfile(D, '*.xlsx'));
Nfiles = length(XLSfile);
MYdata = cell(1, Nfiles);
for k = 1:Nfiles
SName=(sheetnames(XLSfile(k).name));
Nsheets = length(SName);
for jj = 1:Nsheets
MYdata{k,jj} = readtable(XLSfile(k).name, 'Sheet', SName(jj)); % Collects all data
end
end
%% ALT. Way:
% XLSfile = dir('*.xlsx'); % If your current directory, where all MS Excel
% files are residing, then use this
clearvars
XLSfile = dir('*.xlsx');
Nfiles = length(XLSfile);
MYdata = cell(1, Nfiles);
for k = 1:Nfiles
SName=(sheetnames(XLSfile(k).name));
Nsheets = length(SName);
for jj = 1:Nsheets
MYdata{k,jj} = readtable(XLSfile(k).name, 'Sheet', SName(jj)); % Colects all data
end
end
1 commentaire
Steven Lord
le 5 Fév 2023
FYI sheetnames was introduced in release R2019b which came out several months after this question was asked.
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!