Read individual cells from excel and write into excel
조회 수: 20 (최근 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!
댓글 수: 0
답변 (1개)
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")
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!