本文共 903 字,大约阅读时间需要 3 分钟。
本程序主要使matlab批量操作Excel表,其中用到了parfor并行计算加快处理速度。相同效果,速度上和Excel vba
是有差距的。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tic;
clear;
dirs=dir('E:我的文档桌面宝贝销售排行*.csv');
dircell=struct2cell(dirs);
len=length(dircell);
parfor j=1:len
filenames=dircell{1,j};
%%% 读取原始表格数据
[a b c]=xlsread(filenames);
num=size(c,1);
date=c{1}(8:17);
c(1);
d={};
%%% 读取原始表格数据
%%% 取出日期清空第一行
x=' ';
num_str=num2str(num);
range_all=strcat('A1:','Z',num_str);
xlswrite(filenames,x,range_all)
%%% 取出日期清空第一行
for i=2:num
% 空格显示 NAN 被认为是数字
if
~isnumeric(c{i,1})
d=[d;c(i,:)]; %c(i,:) 为原始表格中的第i行的所有元素
end
end
title='日期';
xlswrite(filenames,{title},1,'P1:P1');
num_date=num2str(num-1);
range_p=strcat('P2:','P',num_date);
xlswrite(filenames,{date},1,range_p);
xlswrite(filenames,d,1);
end
toc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
转载地址:http://hhyvx.baihongyu.com/