matlab将不同excel文件中的每一行连接到新的excel文件中

matlab将不同excel文件中的每一行连接到新的excel文件中,excel,matlab,file-io,Excel,Matlab,File Io,我有一个包含10个excel文件的文件夹。每个excel文件包含5张工作表。我想 将每个excel文件第一页中的每一行连接到名为“final”的新excel文件中的新sheet1中 将每个excel文件第一页中的每第二行连接到名为“final”的新excel文件中的新sheet1中 将每个excel文件第1页中的每3行连接到名为“final”的新excel文件中的新sheet1中 然后 将每个excel文件第二页中的每一行连接到名为“final”的新excel文件中的新sheet2中 将每个ex

我有一个包含10个excel文件的文件夹。每个excel文件包含5张工作表。我想

将每个excel文件第一页中的每一行连接到名为“final”的新excel文件中的新sheet1中 将每个excel文件第一页中的每第二行连接到名为“final”的新excel文件中的新sheet1中 将每个excel文件第1页中的每3行连接到名为“final”的新excel文件中的新sheet1中

然后

将每个excel文件第二页中的每一行连接到名为“final”的新excel文件中的新sheet2中 将每个excel文件第二页中的每第二行连接到名为“final”的新excel文件中的新sheet2中 将每个excel文件第2页中的每3行连接到名为“final”的新excel文件中的新sheet2中

重复…对所有5张纸执行

例如:

excel文件1,表1

30  4   1.6 1.2 1.2 1.0
35  16  0.9 0.9 1.5 1.0
40  62  0.9 0.9 1.6 1.2
45  3   0.9 0.9 0.9 0.9
50  1   1.5 1.5 0.8 0.8
excel文件2,表1

10  1   0.8 0.9 0.9 0.9
15  31  0.9 0.9 1.2 1.6
20  2   0.9 0.9 0.9 0.9
25  3   0.9 0.9 0.9 0.9
30  18  0.9 0.9 0.9 0.9
excel文件3,表1至excel文件10,表1等

我想得到的结果

最终版本.xls,第1页

30  4   1.6 1.2 1.2 1.0  %1st row of sheet1 in excel file 1
10  1   0.8 0.9 0.9 0.9  %1st row of sheet1 in excel file 2
... %repeated 1st row of sheet1 in excel file 3 to 10
35  16  0.9 0.9 1.5 1.0    %2nd row of sheet1 in excel file 1
15  31  0.9 0.9 1.2 1.6    %2nd row of sheet1 in excel file 2
... %repeated 2nd row of sheet1 in excel file 3 to 10
最终版本.xls,第2页

%similar to sheet1 just the data read from sheet2..

有人能帮我吗?

我假设您已将每个excel文件保存为名为“file1.csv”、“file2.csv”等的csv文件。
创造性地使用
重塑
命令有助于重新排列行

num_row = 2;
num_col = 6;
num_file = 10;
c = cell(num_file ,1);
for i=1:num_file 
    file = sprintf('file%i.csv', i);
    x = csvread(file);
    c{i} = x'; % transpose so each row is a column
end
data = cell2mat(c);
data = reshape(data, num_col, num_row*num_file;
data = data'; transpose back

我为测试创建了10个
.xls
文件,每个文件有5张纸。所有工作表都有5x6个随机数单元格。这是我的第一个解决方案:

%# get input XLS files
dName = uigetdir('.', 'Select folder containing Excel XLS files');
if dName==0, error('No folder selected'); end
files = dir( fullfile(dName,'*.xls') );
files = strcat(dName, filesep, {files.name}');    %'

%# prepare output XLS file
[fName dName] = uiputfile({'*.xls' 'Excel (*.xls)'}, 'Output File', 'final.xls');
if dName==0, error('No file selected'); end
fOut = fullfile(dName,fName);

%# process
NUM_SHEETS = 5;                       %# number of sheets per file
for s=1:NUM_SHEETS
    %# extract contents of same sheet from all files
    numData = cell(numel(files),1);
    for f=1:numel(files)
        numData{f} = xlsread(files{f}, s);
    end

    %# rearrange data
    numData = cat(3,numData{:});
    numData = reshape(permute(numData,[3 1 2]), [], size(numData,2));

    %# write data to corresponding sheet of output XLS file
    xlswrite(fOut, numData, s);
end
这相当缓慢。大约花了3分钟完成。。。原因是,在对/的每次调用中都会重复创建然后销毁。从好的方面来说,这两个函数隐藏了大量需要完成的脏活,并公开了一个易于使用的界面

在第二个解决方案中,我手动调用。优点是我们只启动它一次,一旦完成就将其拆下,从而消除了大量开销。事实上,此代码在不到4秒内执行!:

%#获取输入XLS文件
dName=uigetdir(“.”,“选择包含Excel XLS文件的文件夹”);
如果dName==0,则出现错误(“未选择文件夹”);结束
files=dir(fullfile(dName,*.xls');
files=strcat(dName,filesep,{files.name}');%
%#获取输出XLS文件
[fName dName]=uiputfile({'*.xls''Excel(*.xls)},'Output File','final.xls');
如果dName==0,则出现错误(“未选择文件”);结束
fOut=fullfile(dName,fName);
%#打开Excel COM服务器
Excel=actxserver('Excel.Application');
Excel.DisplayAlerts=0;
%#准备输出
如果~存在(fOut,'file')
%#如果不存在,则创建
wb=Excel.workbooks.Add;
wb.SaveAs(fOut,1);
wb.关闭(假);
其他的
%#删除现有文件
删除(fOut);
结束
%#提取输入文件的内容
张数=5张;
数据=单元(numel(文件)、NUM_表);
对于f=1:numel(文件)
wb=Excel.Workbooks.Open(文件{f},0,true);%#打开XLS文件进行读取
断言(wb.sheets.Count==NUM_sheets);
对于s=1:NUM_SHEETS%#在所有图纸上循环
%#激活工作表,并提取整个内容
Excel.sheets.get('item',s.Activate();
Excel.Range('A1').Activate();
数据{f,s}=cell2num(Excel.ActiveSheet.UsedRange.Value);
结束
wb.Close(假);%关闭XLS文件
结束
%#重新排列数据
D=单元(数量表,1);
对于s=1:张数
x=cat(3,数据{:,s});
D{s}=重塑(排列(x,[3 1 2]),[],大小(x,2));
结束
%#将数据写入输出XLS文件的工作表
wb=Excel.Workbooks.Open(fOut,0,false);%打开XLS文件进行写入
Excel.Sheets.Count

我相信已经在做类似的事情了…

[fName dName]改为[fNameOut dNameOut]…然后一切都好了…非常感谢。。。
%# get input XLS files
dName = uigetdir('.', 'Select folder containing Excel XLS files');
if dName==0, error('No folder selected'); end
files = dir( fullfile(dName,'*.xls') );
files = strcat(dName, filesep, {files.name}');    %'

%# get output XLS file
[fName dName] = uiputfile({'*.xls' 'Excel (*.xls)'},'Output File','final.xls');
if dName==0, error('No file selected'); end
fOut = fullfile(dName,fName);

%# open Excel COM Server
Excel = actxserver('Excel.Application');
Excel.DisplayAlerts = 0;

%# prepare output
if ~exist(fOut, 'file')
    %# create if doesnt exist
    wb = Excel.workbooks.Add;
    wb.SaveAs(fOut,1);
    wb.Close(false);
else
    %# delete existing file
    delete(fOut);
end

%# extract contents of input files
NUM_SHEETS = 5;
data = cell(numel(files),NUM_SHEETS);
for f=1:numel(files)
    wb = Excel.Workbooks.Open(files{f}, 0, true); %# open XLS file for reading
    assert( wb.sheets.Count == NUM_SHEETS );
    for s=1:NUM_SHEETS                            %# loop over all sheets
        %# activate sheet, and extract entire content
        Excel.sheets.get('item',s).Activate();
        Excel.Range('A1').Activate();
        data{f,s} = cell2num( Excel.ActiveSheet.UsedRange.Value );
    end
    wb.Close(false);                              %# close XLS file
end

%# rearrange data
D = cell(NUM_SHEETS,1);
for s=1:NUM_SHEETS
    x = cat(3,data{:,s});
    D{s} = reshape(permute(x,[3 1 2]), [], size(x,2));
end

%# write data to sheets of output XLS file
wb = Excel.Workbooks.Open(fOut, 0, false);       %# open XLS file for writing
while Excel.Sheets.Count < NUM_SHEETS            %# create sheets as required
    Excel.Sheets.Add([], Excel.Sheets.Item(Excel.Sheets.Count));
end
for s=1:NUM_SHEETS                               %# write conents to each sheet
    cellRange = sprintf('A1:%s%d', 'A'+size(D{s},2)-1, size(D{s},1));
    wb.sheets.get('item',s).Activate();
    Excel.Range(cellRange).Select();
    set(Excel.selection, 'Value',num2cell(D{s}));
end
wb.Save();
wb.Close(false);                                 %# close XLS file

%# cleanup
Excel.Quit();
Excel.delete();
clear Excel;