Read individual cells from excel and write into excel

조회 수: 20 (최근 30일)
Mary
Mary 2023년 10월 9일
답변: Mario Malic 2023년 10월 9일
I have several hundred excel sheets with three cells (B7, G11, D56) of text I'd like to write into one combined excel file.
I tried using the -1 signifier to select the multiple cells but it only wrote the first cell I selected into the new file.
[~,txt] = xlsread(['sourcefile.xls'],-1);
x= txt;
xlswrite('destfile.xls',x,1,'A');
The source files are not named in any sort of series so I know I will probably need a copy of this with every source file name listed but i'm not sure (A) how to write multiple cells or (B) how to indicate their destination in a way that doesn't overwrite the rows.
Thanks in advance!

답변 (1개)

Mario Malic
Mario Malic 2023년 10월 9일
Try this
clear;
files = dir("*.xlsx");
idx = contains({files.name}, "~"); % removes open Excel files from list
files(idx) = [];
numFiles = numel(files);
filePath = cell(numFiles, 1);
data = cell(numFiles, 3);
for i = 1 : numFiles
filePath{i} = fullfile(files(i).folder, files(i).name);
data{i, 1} = readmatrix(filePath{i}, "Range", "B47:B47");
data{i, 2} = readmatrix(filePath{i}, "Range", "G11:G11");
data{i, 3} = readmatrix(filePath{i}, "Range", "D56:D56");
end
excelData = [filePath, data];
writecell(excelData, "output.xlsx")

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by