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

10 views (last 30 days)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 7 Aug 2021
Edited: Walter Roberson on 7 Aug 2021
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 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!