Appending multiple Excel files using script

8 views (last 30 days)
Hello everyone! I want to append 500 excel files. IS there any way through coding to complete the desired task. Any help or suggestion would be highly appreciated.
Thanks
  5 Comments
Rik
Rik on 16 Jan 2022
It didn't answer the entire question, but it did give you a hint where to start.
What have you tried?
Sugar Mummy
Sugar Mummy on 16 Jan 2022
Hi @Rik, I have tried the first method given in the link but as I am using MATLAB online I have no idea how to process it further after uploading it on MATLAB drive and how to append the files. Do I have to create variable for each file.
Please guide. Thanks!

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Jan 2022
input_dir = '/MATLAB Drive/Project11/xin'; %directory excel files are stored in
input_extension = '.xlsx'; %or .xls
output_file = '/MATLAB Drive/Project11/combined.xlsx'; %name of output file in a different directory
dinfo = dir( fullfile(input_dir, "*" + input_extension) );
filenames = fullfile({dinfo.folder}, {dinfo.name});
nfiles = length(filenames);
for K = 1 : nfiles
data = readcell(filenames{K});
if K == 1
writecell(data, output_file, 'writemode', 'overwritesheet');
else
writecell(data, output_file, 'writemode', 'append');
end
end
Assumptions:
  1. Only sheet 1 of the data is to be copied
  2. all of the sheet is to be copied every time. So if there is a common header for each file, the header will appear each time, not just at the top of the new file
  3. the code does not assume that the files have the same number or type of variables. If one has (say) 7 columns and the next has (say) 11 columns, then the code is fine with that
  4. any macros in the files will be copied, but no attempt is made to fix-up cell references
  5. the order copied will be internal directory order as returned by the operating system. If the files are named inconsistently such as Sand1.xlsx Sand2.xlsx ... Sand9.xlsx Sand10.xlsx Sand11.xlsx ... then the directory order would typically be Sand1.xlsx Sand10.xlsx Sand11.xlsx ... Sand19.xlsx Sand2.xlsx Sand20.xlsx ... This code makes no attempt to order the files numerically
  6. The code makes no attempt to add information into the composite file in order to mark the boundaries between files or in order to indicate which file the data was originally from
  7. As you asked about "append" rather than "merge", the data is just all put one file after another -- not, for example, using different sheets for each different file.
  3 Comments
Walter Roberson
Walter Roberson on 16 Jan 2022
writetable() was documented as caching the connection, so I would imagine that writecell() does as well.
Sugar Mummy
Sugar Mummy on 17 Jan 2022
Thank you so much @Walter Roberson for the detailed answer.
YOU ARE SO KIND!

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!