Adding multiple excel sheets to one excel workbook

조회 수: 4 (최근 30일)
Courtney Rich
Courtney Rich 2020년 7월 15일
댓글: Walter Roberson 2020년 7월 16일
I have an excel workbook that does some data processing and calculations on data currently have to manually add all the data to the work book.
I would like to be able to auto populate the excel work book with the data that i need for the calculations. I currently have 3 excel files with the data i need (lets call them results1, reults 2 and results 3). I have to add this data into the one excel workbook (lets call it data_processing), I want to add each results data into its own specific sheet in the work book. So i have muliptle sheets in the data_proessing workbook i want 3 sheets to be the results and then the other sheets i have already made and they take the data from the reults sheets and do some calculations.
If i put all the excel files into one locatin is there a way to get matlab to autopopulate the excel workbook with the data and in the specific sheet i want it to go to?
Thanks!!

답변 (1개)

Walter Roberson
Walter Roberson 2020년 7월 15일
Sure, you can writetable() or writematrix() or writecell() specifying 'Sheet' .
The hardest part would probably be figuring out what the appropriate sheet name would be for a given file.
inputdir = 'appropriate directory name';
outputfile = 'appropriate file name'; %not inside inputdir
dinfo = dir( fullfile(inputdir, '*.xlsx') );
filenames = fullfile( {dinfo.folder}, {dinfo.name} );
for K = 1 : length(filenames)
thisfile = filenames{K};
[~, basename, ~] = fileparts(thisfile);
sheetname = ['sheet_for_', basename]; %adjust as appropriate
thiscontent = readcell(thisfile);
writecell(thiscontent, outputfile, 'Sheet', sheetname);
end
  댓글 수: 2
Courtney Rich
Courtney Rich 2020년 7월 16일
Is there a away that instead of doing a loop i just specifically call out each of the file names that i want it to read and then what the sheet name is cale dthat i want it to put the data? Since its only 3 files and i need them in specific spots.
Thanks!
Walter Roberson
Walter Roberson 2020년 7월 16일
filenames = {'results1.xlsx', 'reults 2.xlsx', 'results 3.xlsx'};
sheetnames = {'StarTrek', 'StarWars', 'DancingWithTheStars'};
for K = 1 : length(filenames)
thisfile = filenames{K};
sheetname = sheetnames{K};
thiscontent = readcell(thisfile);
writecell(thiscontent, outputfile, 'Sheet', sheetname);
end

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

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by