필터 지우기
필터 지우기

Write multiple excel files into one excel workbook sheet by sheet.

조회 수: 1 (최근 30일)
I have 40 excel files (.xlsx') with different names in a folder on desktop. I want to write them into one excel workbook named 'mydata.xlsx' containing 40 sheets with each sheet bear the names of each of the files. Kindly help me with the codes. Thanks.

채택된 답변

Walter Roberson
Walter Roberson 2021년 8월 7일
편집: Walter Roberson 2021년 8월 7일
outfile = 'mydata.xlsx';
dinfo = dir('*.xlsx');
filenames = setdiff({dinfo.name}, outfile);
for K = 1 : length(filenames)
thisfile = filenames{K};
[~, basename, ~] = fileparts(thisfile);
T = readtable(thisfile);
writetable(T, outfile, 'sheet', basename);
end
  댓글 수: 5
Walter Roberson
Walter Roberson 2021년 8월 7일
편집: Walter Roberson 2021년 8월 7일
l guess it is because we use readtable and writetable.
That seems unlikely to be the reason, but you can try xlsread() / xlswrite() anyhow.
outfile = 'mydata.xlsx';
dinfo = dir('*.xlsx');
filenames = setdiff({dinfo.name}, outfile);
for K = 1 : length(filenames)
thisfile = filenames{K};
[~, basename, ~] = fileparts(thisfile);
[~, ~, raw] = xlsread(thisfile);
xlswrite(outfile, raw, basename);
end
Ojo Olusola
Ojo Olusola 2021년 8월 8일
Thank you. This code now work efficiently.

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by