Appending multiple Excel files using script

조회 수: 11 (최근 30일)
Sugar Mummy
Sugar Mummy 2022년 1월 16일
댓글: Sugar Mummy 2022년 1월 17일
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
Rik
Rik 2022년 1월 16일
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 2022년 1월 16일
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!

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

채택된 답변

Walter Roberson
Walter Roberson 2022년 1월 16일
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
Walter Roberson
Walter Roberson 2022년 1월 16일
writetable() was documented as caching the connection, so I would imagine that writecell() does as well.
Sugar Mummy
Sugar Mummy 2022년 1월 17일
Thank you so much @Walter Roberson for the detailed answer.
YOU ARE SO KIND!

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by