Read individual cells from excel and write into excel

25 次查看(过去 30 天)
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")

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by