Read individual cells from excel and write into excel
21 views (last 30 days)
Show older comments
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!
0 Comments
Answers (1)
Mario Malic
on 9 Oct 2023
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")
0 Comments
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!