How to merge multiple xls files into a single file with the same worksheet?
2 次查看(过去 30 天)
显示 更早的评论
Hi. I want to ask about how to merge multiple xls files into a single file with the same worksheet. I read some questions which also have the same problem, but mostly the resulted file has separate worksheets. In my case, I want to merge the table so that it only has one worksheet. I try this code below:
[files,folder] = uigetfile('*.xls','Select Files','MultiSelect','on');
output = fullfile(folder,'rainfall.xls');
addpath(dir);
for i = 1:numel(files)
ftab = readtable(files{i});
writetable(ftab,output,'Sheet',files{i});
end
but of course it has separated worksheets. I want to make it in one worksheet as the files have also the same structure so it should be easily to merge. Could you please help me? I really appreciate for your help.
0 个评论
回答(1 个)
dpb
2017-11-12
Probably simplest presuming the individual sheets aren't terribly big is to loop through and accumulate all the data into one array and then write it at the end to the target worksheet. This is minimum number of nFiles reads and one write.
Otherwise, to do sequentially you'll have to keep track of location in the sheet to write each subsequent dataset to and it's both nFiles reads and writes.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!