matlab 將統計年鑑xls中的部分數據從新整理到一個表xlsx中——excel的建立刪除、sheet重命名、xls數據讀取寫入

將統計年鑑xls中的部分數據從新整理到一個表xlsx中

前言

前言:這其中涉及matlab對excel的建立刪除,sheet名的修改,從數據源的excel中讀取數據,再將其寫入新的xlsx中。其中有不少細節須要注意,具體見代碼。java

代碼

該代碼的目的是從原來的文件中讀取四類做物的播種面積、單產量和總產量三項數據。
原始數據:
在這裏插入圖片描述
結果文件:
在這裏插入圖片描述
打開後顯示:
在這裏插入圖片描述





數組

clc;
clear;
t0 = cputime;
%----------------need to change based on your condition--------------------
data_dir = 'E:\you\數據源\';
output_dir = 'E:\you\';
%data_dir爲數據源所在的位置
%output_dir爲統計結果文件的位置,如2101豆類縣級統計數據.xlsx文件所在位置
%--------------------------------------------------------------------------
excelpaths=[string([output_dir,'2101玉米縣級統計數據.xlsx']),string([output_dir,'2101豆類縣級統計數據.xlsx']), ...
        string([output_dir,'2101小麥縣級統計數據.xlsx']),string([output_dir,'2101水稻縣級統計數據.xlsx'])];
%若是文件存在則刪除
if exist(char(excelpaths(1)),'file') == 2
   delete(char(excelpaths(1))) ;
end
if exist(char(excelpaths(2)),'file') == 2
   delete(char(excelpaths(2))) ;
end
if exist(char(excelpaths(3)),'file') == 2
   delete(char(excelpaths(3))) ;
end
if exist(char(excelpaths(4)),'file') == 2
   delete(char(excelpaths(4))) ;
end
xlswrite(char(excelpaths(1)),1);
xlswrite(char(excelpaths(2)),1);
xlswrite(char(excelpaths(3)),1);
xlswrite(char(excelpaths(4)),1);
%--------------------------------------------------------------------------
%                           遍歷全部播種面積表
%找出玉米、小麥、水稻、豆類 全部地區該年的數據 放入玉米新表的播種面積列中,關閉新表格
%--------------------------------------------------------------------------
dirs=dir([data_dir,'*播種面積*2*.xls']);
dircell=struct2cell(dirs);
bozhong_fns_string = string(dircell(1,:));
[lines,samples]=size(bozhong_fns_string);
%fn=dircell(1,1)
sheet_id=1;%標記excel中sheet順序,寫入數據時需修改sheet名稱
for i=2000:2016
    judge_cell = strfind(bozhong_fns_string,string(i));
    %第一個查找的元素爲播種面積
    for num = 1:samples
       temp = isempty(judge_cell{num});
       %判斷哪一個cell不爲空,不爲空的就是找到的文件名
        if  temp == 0 
            selected_fn = bozhong_fns_string(num);
            break
        else 
            selected_fn = '';
        end
    end
    %以上,找到播種面積文件,下面開始讀取相應類別的數據,若是找不到,則賦文件名爲空
    if isempty(selected_fn)==1
        continue
    %不然,執行如下步驟: 
    end
    %----------------------------------------------------------------------
    %1豆類2玉米3水稻4小麥
    %打開數據源的表
    [numi,txti,rawi]=xlsread([data_dir,char(selected_fn)]);
    %----------------------------------------------------------------------
    %‘項目’or‘指標’那一行爲城市名稱,選擇讀取該行放置到新表的第一列中
    temp = char(rawi{:,1});
    temp_lines = size(temp,1);
    %獲取城市名行號,j
    for j=1:temp_lines
        new_temp = temp(j,:);
        new_temp(find(isspace(new_temp)))=[];
        %new_temp(j,:)=strrep(temp(j,:),' ','');
        %strrep返回數組維度上的長度已經改變,不能賦值給原來的變量了,應該設置新變量
        second_temp = strfind(string(new_temp),string('指標'));
        third_temp = isempty(second_temp);
        forth_temp = strfind(string(new_temp),string('項目'));
        fifth_temp = isempty(forth_temp);
        if third_temp==0 || fifth_temp==0
           break 
        end
    end
    
    %獲取玉米播種面積行號,k
    for k=1:temp_lines
        new_temp = temp(k,:);
        %new_temp(new_temp==' ')='';
        new_temp(find(isspace(new_temp)))=[];
        second_temp = strfind(string(new_temp),string('玉'));
        third_temp = isempty(second_temp);
        if  third_temp==0
           break 
        end
    end
    %獲取豆類合計播種面積行號,mm
    for m=1:temp_lines
        new_temp = temp(m,:);
        %new_temp(new_temp==' ')='';
        new_temp(find(isspace(new_temp)))=[];
        second_temp = strfind(string(new_temp),string('豆類'));
        third_temp = isempty(second_temp);
        forth_temp = strfind(string(new_temp),string('大豆'));
        fifth_temp = isempty(forth_temp);
        if  third_temp==0
            mm=m;
           break 
        elseif fifth_temp==0
            mm=m;
            break
        end
    end
    %獲取小麥播種面積行號,有小麥的計小麥,沒有小麥的計春小麥,nn
    for n=1:temp_lines
        new_temp = temp(n,:);
        %new_temp(new_temp==' ')='';
        new_temp(find(isspace(new_temp)))=[];
        second_temp = strfind(string(new_temp),string('小麥'));
        third_temp = isempty(second_temp);
        if third_temp==0 && size(new_temp,2)==2
           nn=n;
           break 
        elseif third_temp==0 && size(new_temp,2)==3
           nn=n;
           break
        end
    end
    %獲取水稻播種面積行號,有水稻的,計水稻;只有稻穀的,計稻穀,oo
    for o=1:temp_lines
        new_temp = temp(o,:);
        %new_temp(new_temp==' ')='';
        new_temp(find(isspace(new_temp)))=[];
        second_temp = strfind(string(new_temp),string('稻穀'));
        third_temp = isempty(second_temp);
        forth_temp = strfind(string(new_temp),string('水稻'));
        fifth_temp = isempty(forth_temp);
        if third_temp==0
           oo=o;
           break 
        elseif fifth_temp==0 && size(new_temp,2)==2
           oo=o;
           break
        elseif fifth_temp==0 && size(new_temp,2)~=2
            oo=o;
            break
        end
    end
    %----------------------------------------------------------------------
    %將數據寫入excel中
    %----------------------------------------------------------------------
    %修改sheet名稱
    
    e = actxserver('Excel.Application');
    for p=1:4
        if sheet_id <= 3 
            ewbp = e.Workbooks.Open(char(excelpaths(p)));  
            ewbp.Worksheets.Item(sheet_id).Name = num2str(i);
            ewbp.Save;                              
            ewbp.Close(false);  
        end
    end
    sheet_id = sheet_id+1;
    e.Quit;
    %有多少個城市
    city_num = size(char(txti{j,:}),1);%j爲城市的行號
    output = cell(city_num,2);
    %不能一次賦值全部數據到output
    %output{:,1}= txti{j,:};
    %output{:,2}= txti{k,:};
    %城市&玉米
    for q=1:city_num
        if q==1
           output{q,1}='城市'; 
           output{q,2}='播種面積';
        else
           new_temp1 = rawi{j,q};
           new_temp1(find(isspace(new_temp1)))=[];
           new_temp2 = rawi{k,q};
           new_temp2(find(isspace(new_temp2)))=[];
           new_temp2(find(isnan(new_temp2)))=[];
           output{q,1} = new_temp1;
           output{q,2} = str2double(string(new_temp2));
        end     
    end
    xlswrite(char(excelpaths(1)),output,char(string(i)),['A1:B',char(string(city_num))]);
    
    %城市&豆類
    for q=1:city_num
        if q==1
           output{q,1}='城市'; 
           output{q,2}='播種面積';
        else
           new_temp1 = rawi{j,q};
           new_temp1(find(isspace(new_temp1)))=[];
           new_temp2 = rawi{mm,q};
           new_temp2(find(isspace(new_temp2)))=[];
           new_temp2(find(isnan(new_temp2)))=[];
           output{q,1} = new_temp1;
           output{q,2} = str2double(string(new_temp2));
           %disp(output)
        end     
    end
    xlswrite(char(excelpaths(2)),output,char(string(i)),['A1:B',char(string(city_num))]);

    %城市&小麥
    for q=1:city_num
        if q==1
           output{q,1}='城市'; 
           output{q,2}='播種面積';
        else
           new_temp1 = rawi{j,q};
           new_temp1(find(isspace(new_temp1)))=[];
           new_temp2 = rawi{nn,q};
           new_temp2(find(isspace(new_temp2)))=[];
           new_temp2(find(isnan(new_temp2)))=[];
           output{q,1} = new_temp1;
           output{q,2} = str2double(string(new_temp2));
        end     
    end
    xlswrite(char(excelpaths(3)),output,char(string(i)),['A1:B',char(string(city_num))]);
    
    %城市&水稻
    for q=1:city_num
        if q==1
           output{q,1}='城市'; 
           output{q,2}='播種面積';
        else
           new_temp1 = rawi{j,q};
           new_temp1(find(isspace(new_temp1)))=[];
           new_temp2 = rawi{oo,q};
           new_temp2(find(isspace(new_temp2)))=[];
           new_temp2(find(isnan(new_temp2)))=[];
           output{q,1} = new_temp1;
           output{q,2} = str2double(string(new_temp2));
        end     
    end
    xlswrite(char(excelpaths(4)),output,char(string(i)),['A1:B',char(string(city_num))]);   
    
end






%--------------------------------------------------------------------------
%                             將單產寫入excel
%--------------------------------------------------------------------------
dirs=dir([data_dir,'*單產量*2*.xls']);
dircell=struct2cell(dirs);
perout_fns_string = string(dircell(1,:));
[lines,samples]=size(perout_fns_string);
%fn=dircell(1,1)
for i=2000:2016
    judge_cell = strfind(perout_fns_string,string(i));
    %第二個查找的元素爲單產量
    for num = 1:samples
       temp = isempty(judge_cell{num});
       %判斷哪一個cell不爲空,不爲空的就是找到的文件名
        if  temp == 0 
            selected_fn = perout_fns_string(num);
            break
        else 
            selected_fn = '';
            %若是找不到,則賦文件名爲空
        end
    end
    %以上,找到單產量文件,下面開始讀取相應類別的數據
    if isempty(selected_fn)==1
        continue
    %不然,執行如下步驟: 
    end
    %----------------------------------------------------------------------
    %1豆類2玉米3水稻4小麥
    %打開數據源的表
    [numi,txti,rawi]=xlsread([data_dir,char(selected_fn)]);
    %----------------------------------------------------------------------
    %‘項目’or‘指標’那一行爲城市名稱,選擇讀取該行放置到新表的第一列中
    temp = char(rawi{:,1});
    temp_lines = size(temp,1);
    %獲取城市名行號,j
    for j=1:temp_lines
        new_temp = temp(j,:);
        new_temp(find(isspace(new_temp)))=[];
        %new_temp(j,:)=strrep(temp(j,:),' ','');
        %strrep返回數組維度上的長度已經改變,不能賦值給原來的變量了,應該設置新變量
        second_temp = strfind(string(new_temp),string('指標'));
        third_temp = isempty(second_temp);
        forth_temp = strfind(string(new_temp),string('項目'));
        fifth_temp = isempty(forth_temp);
        if third_temp==0 || fifth_temp==0
           break 
        end
    end
    
    %獲取玉米單產行號,k
    for k=1:temp_lines
        new_temp = temp(k,:);
        %new_temp(new_temp==' ')='';
        new_temp(find(isspace(new_temp)))=[];
        second_temp = strfind(string(new_temp),string('玉'));
        third_temp = isempty(second_temp);
        if  third_temp==0
           break 
        end
    end
    %獲取豆類合計單產行號,mm
    for m=1:temp_lines
        new_temp = temp(m,:);
        %new_temp(new_temp==' ')='';
        new_temp(find(isspace(new_temp)))=[];
        second_temp = strfind(string(new_temp),string('豆類'));
        third_temp = isempty(second_temp);
        forth_temp = strfind(string(new_temp),string('大豆'));
        fifth_temp = isempty(forth_temp);
        if  third_temp==0
            mm=m;
           break 
        elseif fifth_temp==0
            mm=m;
            break
        end
    end
    %獲取小麥單產行號,有小麥的計小麥,沒有小麥的計春小麥,nn
    for n=1:temp_lines
        new_temp = temp(n,:);
        %new_temp(new_temp==' ')='';
        new_temp(find(isspace(new_temp)))=[];
        second_temp = strfind(string(new_temp),string('小麥'));
        third_temp = isempty(second_temp);
        if third_temp==0 && size(new_temp,2)==2
           nn=n;
           break 
        elseif third_temp==0 && size(new_temp,2)==3
           nn=n;
           break
        end
    end
    %獲取水稻單產行號,有水稻的,計水稻;只有稻穀的,計稻穀,oo
    for o=1:temp_lines
        new_temp = temp(o,:);
        %new_temp(new_temp==' ')='';
        new_temp(find(isspace(new_temp)))=[];
        second_temp = strfind(string(new_temp),string('稻穀'));
        third_temp = isempty(second_temp);
        forth_temp = strfind(string(new_temp),string('水稻'));
        fifth_temp = isempty(forth_temp);
        if third_temp==0
           oo=o;
           break 
        elseif fifth_temp==0 && size(new_temp,2)==2
           oo=o;
           break
        elseif fifth_temp==0 && size(new_temp,2)~=2
            oo=o;
            break
        end
    end
    %----------------------------------------------------------------------
    %將數據寫入excel中
    %----------------------------------------------------------------------
    %有多少個城市
    city_num = size(char(txti{j,:}),1);%j爲城市的行號
    output = cell(city_num,1);
    %不能一次賦值全部數據到output
    %output{:,1}= txti{j,:};
    %output{:,2}= txti{k,:};
    %城市&玉米
    for q=1:city_num
        if q==1
           output{q,1}='單產量'; 
        else
           new_temp2 = rawi{k,q};
           new_temp2(find(isspace(new_temp2)))=[];
           new_temp2(find(isnan(new_temp2)))=[];
           output{q,1} = str2double(string(new_temp2));
        end     
    end
    xlswrite(char(excelpaths(1)),output,char(string(i)),['C1:C',char(string(city_num))]);
    
    %城市&豆類
    for q=1:city_num
        if q==1
           output{q,1}='單產量'; 
        else
           new_temp2 = rawi{mm,q};
           new_temp2(find(isspace(new_temp2)))=[];
           new_temp2(find(isnan(new_temp2)))=[];
           
           output{q,1} = str2double(string(new_temp2));
           %disp(output)
        end     
    end
    xlswrite(char(excelpaths(2)),output,char(string(i)),['C1:C',char(string(city_num))]);

    %城市&小麥
    for q=1:city_num
        if q==1
           output{q,1}='單產量'; 
        else
           
           new_temp2 = rawi{nn,q};
           new_temp2(find(isspace(new_temp2)))=[];
           new_temp2(find(isnan(new_temp2)))=[];
           
           output{q,1} = str2double(string(new_temp2));
        end     
    end
    xlswrite(char(excelpaths(3)),output,char(string(i)),['C1:C',char(string(city_num))]);
    
    %城市&水稻
    for q=1:city_num
        if q==1
           output{q,1}='單產量'; 
        else
           
           new_temp2 = rawi{oo,q};
           new_temp2(find(isspace(new_temp2)))=[];
           new_temp2(find(isnan(new_temp2)))=[];
           
           output{q,1} = str2double(string(new_temp2));
        end     
    end
    xlswrite(char(excelpaths(4)),output,char(string(i)),['C1:C',char(string(city_num))]);   
    
end





%--------------------------------------------------------------------------
%                             將總產寫入excel
%--------------------------------------------------------------------------
dirs=dir([data_dir,'*總產量*2*.xls']);
dircell=struct2cell(dirs);
totalout_fns_string = string(dircell(1,:));
[lines,samples]=size(totalout_fns_string);
%fn=dircell(1,1)
for i=2000:2016
    judge_cell = strfind(totalout_fns_string,string(i));
    %第三個查找的元素爲總產量
    for num = 1:samples
       temp = isempty(judge_cell{num});
       %判斷哪一個cell不爲空,不爲空的就是找到的文件名
        if  temp == 0 
            selected_fn = totalout_fns_string(num);
            break
        else 
            selected_fn = '';
            %若是找不到,則賦文件名爲空
        end
    end
    %以上,找到總產量文件,下面開始讀取相應類別的數據
    if isempty(selected_fn)==1
        continue
    %不然,執行如下步驟: 
    end
    %----------------------------------------------------------------------
    %1豆類2玉米3水稻4小麥
    %打開數據源的表
    [numi,txti,rawi]=xlsread([data_dir,char(selected_fn)]);
    %----------------------------------------------------------------------
    %‘項目’or‘指標’那一行爲城市名稱,選擇讀取該行放置到新表的第一列中
    temp = char(rawi{:,1});
    temp_lines = size(temp,1);
    %獲取城市名行號,j
    for j=1:temp_lines
        new_temp = temp(j,:);
        new_temp(find(isspace(new_temp)))=[];
        %new_temp(j,:)=strrep(temp(j,:),' ','');
        %strrep返回數組維度上的長度已經改變,不能賦值給原來的變量了,應該設置新變量
        second_temp = strfind(string(new_temp),string('指標'));
        third_temp = isempty(second_temp);
        forth_temp = strfind(string(new_temp),string('項目'));
        fifth_temp = isempty(forth_temp);
        if third_temp==0 || fifth_temp==0
           break 
        end
    end
    
    %獲取玉米總產行號,k
    for k=1:temp_lines
        new_temp = temp(k,:);
        %new_temp(new_temp==' ')='';
        new_temp(find(isspace(new_temp)))=[];
        second_temp = strfind(string(new_temp),string('玉'));
        third_temp = isempty(second_temp);
        if  third_temp==0
           break 
        end
    end
    %獲取豆類合計總產行號,mm
    for m=1:temp_lines
        new_temp = temp(m,:);
        %new_temp(new_temp==' ')='';
        new_temp(find(isspace(new_temp)))=[];
        second_temp = strfind(string(new_temp),string('豆類'));
        third_temp = isempty(second_temp);
        forth_temp = strfind(string(new_temp),string('大豆'));
        fifth_temp = isempty(forth_temp);
        if  third_temp==0
            mm=m;
           break 
        elseif fifth_temp==0
            mm=m;
            break
        end
    end
    %獲取小麥總產行號,有小麥的計小麥,沒有小麥的計春小麥,nn
    for n=1:temp_lines
        new_temp = temp(n,:);
        %new_temp(new_temp==' ')='';
        new_temp(find(isspace(new_temp)))=[];
        second_temp = strfind(string(new_temp),string('小麥'));
        third_temp = isempty(second_temp);
        if third_temp==0 && size(new_temp,2)==2
           nn=n;
           break 
        elseif third_temp==0 && size(new_temp,2)==3
           nn=n;
           break
        end
    end
    %獲取水稻總產行號,有水稻的,計水稻;只有稻穀的,計稻穀,oo
    for o=1:temp_lines
        new_temp = temp(o,:);
        %new_temp(new_temp==' ')='';
        new_temp(find(isspace(new_temp)))=[];
        second_temp = strfind(string(new_temp),string('稻穀'));
        third_temp = isempty(second_temp);
        forth_temp = strfind(string(new_temp),string('水稻'));
        fifth_temp = isempty(forth_temp);
        if third_temp==0
           oo=o;
           break 
        elseif fifth_temp==0 && size(new_temp,2)==2
           oo=o;
           break
        elseif fifth_temp==0 && size(new_temp,2)~=2
            oo=o;
            break
        end
    end
    %----------------------------------------------------------------------
    %將數據寫入excel中
    %----------------------------------------------------------------------
    %有多少個城市
    city_num = size(char(txti{j,:}),1);%j爲城市的行號
    output = cell(city_num,1);
    %不能一次賦值全部數據到output
    %output{:,1}= txti{j,:};
    %output{:,2}= txti{k,:};
    %城市&玉米
    for q=1:city_num
        if q==1
           output{q,1}='總產量'; 
        else
           new_temp2 = rawi{k,q};
           new_temp2(find(isspace(new_temp2)))=[];
           new_temp2(find(isnan(new_temp2)))=[];
           output{q,1} = str2double(string(new_temp2));
        end     
    end
    xlswrite(char(excelpaths(1)),output,char(string(i)),['D1:D',char(string(city_num))]);
    
    %城市&豆類
    for q=1:city_num
        if q==1
           output{q,1}='總產量'; 
        else
           new_temp2 = rawi{mm,q};
           new_temp2(find(isspace(new_temp2)))=[];
           new_temp2(find(isnan(new_temp2)))=[];
           
           output{q,1} = str2double(string(new_temp2));
           %disp(output)
        end     
    end
    xlswrite(char(excelpaths(2)),output,char(string(i)),['D1:D',char(string(city_num))]);

    %城市&小麥
    for q=1:city_num
        if q==1
           output{q,1}='總產量'; 
        else
           
           new_temp2 = rawi{nn,q};
           new_temp2(find(isspace(new_temp2)))=[];
           new_temp2(find(isnan(new_temp2)))=[];
           
           output{q,1} = str2double(string(new_temp2));
        end     
    end
    xlswrite(char(excelpaths(3)),output,char(string(i)),['D1:D',char(string(city_num))]);
    
    %城市&水稻
    for q=1:city_num
        if q==1
           output{q,1}='總產量'; 
        else
           
           new_temp2 = rawi{oo,q};
           new_temp2(find(isspace(new_temp2)))=[];
           new_temp2(find(isnan(new_temp2)))=[];
           
           output{q,1} = str2double(string(new_temp2));
        end     
    end
    xlswrite(char(excelpaths(4)),output,char(string(i)),['D1:D',char(string(city_num))]);   
    
end
disp('統計完成,結束運行!');
t1 = cputime;
during = t1 - t0;
disp('耗時:');
disp(during);
相關文章
相關標籤/搜索