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 Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by