Count number of sheets in excel file

조회 수: 91 (최근 30일)
Michael Whipple
Michael Whipple 2019년 2월 5일
이동: Image Analyst 2023년 2월 6일
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.

답변 (3개)

KSSV
KSSV 2019년 2월 5일
[status,sheets] = xlsfinfo(filename)

laurent jalabert
laurent jalabert 2022년 2월 4일
이동: Image Analyst 2023년 2월 6일
d = uigetdir(pwd, 'Select a folder');
DATA = dir(fullfile(d, '*.xlsx'));
[status,sheets] = xlsfinfo(DATA.name);
sheets = sheetnames(DATA.name);
length(sheets)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 2월 5일
편집: Sulaymon Eshkabilov 2023년 2월 5일
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
Steven Lord
Steven Lord 2023년 2월 5일
FYI sheetnames was introduced in release R2019b which came out several months after this question was asked.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

태그

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by