Merge all '.xlsx' files in a folder

조회 수: 1 (최근 30일)
Mekala balaji
Mekala balaji 2016년 2월 26일
댓글: Chakradhar Rao Tandule 2018년 6월 16일
I have multiple '.xlsx' files in a folder, and I want to merge them all.
my input and out folder paths are below:
sourcefolder = 'D:\OutputFolder';
destinationfolder = 'D:\mergeddata';
But I dont know how to merge them. Kindly someone help me. Sincerely,

채택된 답변

Walter Roberson
Walter Roberson 2016년 2월 26일
I notice that the input files have three sheets, with two of them empty. I decided that it was potentially important to merge each sheet separately, by name; otherwise the code would be much shorter.
Note: the code will fail to distinguish between sheetnames which are greater than 63 characters long (or, in some cases, even shorter.)
sourcefolder = 'D:\OutputFolder';
destinationfolder = 'D:\mergeddata';
dinfo = dir( fullfile(sourcefolder, '*.xlsx') );
numfile = length(dinfo);
input_data = struct([]);
input_sheets = struct([]);
for K = 1 : numfile
thisfile = fullfile(sourcefolder, dinfo(K).name );
[status, sheets] = xlsfinfo(thisfile);
for sn = 1 : length(sheets)
[~, ~, raw] = xlsread(thisfile, sn);
sheetname = sheets{sn};
sheet_var = genvarname(sheetname);
if ~isfield(input_data, sheet_var)
input_data.(sheet_var) = {};
input_sheets.(sheet_var) = sheetname;
end
input_data.(sheet_var) = [input_data.(sheet_var); raw]
end
end
output_file = fullfile( destinationfolder, 'merged_files.xlsx');
sheet_vars = fieldnames(input_sheets);
for sn = 1 : length(sheet_vars)
sheet_var = sheet_vars{sn};
data_to_write = input_data.(sheet_var);
sheetname = input_sheets.(sheet_var);
xlswrite( output_file, data_to_write, sheetname );
end
  댓글 수: 5
Walter Roberson
Walter Roberson 2018년 6월 15일
xlswrite( output_file, data_to_write.', sheetname );
Chakradhar Rao Tandule
Chakradhar Rao Tandule 2018년 6월 16일
This is not i want to do so sir... i have attached the intput files and output file for your kind reference.. Please help me to get such format output file using the given inputs...

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

추가 답변 (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