function str=letter2char(num)
s=num+64;
str=char(s);%因为char(65)=A
end
上述自定义函数可以实现由1转化为A,第二个自定义函数如下:
function s=lettertoxls(Integer0)%matlab中的函数定义
if(Integer0<=26)%若只有单个字母
s=letter2char(Integer0);%转化成字母
elseif(Integer0<=702&& Integer0>26)%不止单个字母,比如AA,BF等。702刚好到两位的最后一个列号:ZZ
Integer1 =fix(Integer0/26);%取商
ifmod(Integer0,26)==0
Integer1 =fix(Integer0/26)-1;%避免出现从AY直接到BZ的现象
end
while(Integer0>26)
Integer0=Integer0-26;
end
a=letter2char(Integer1);
b=letter2char(Integer0);
s=strcat(a,b);
end
end
clear;[data,str]=xlsread("C:\Users\吴宇航\Desktop\新建文件夹\8月份日排产-成品.xls");%这里打开是为了在表格中筛选符合条件的数据
e =actxserver('Excel.Application');%这里是建立服务端('Excel.Application'是指excel的服务端,和上面打开表格不重复不矛盾)
ewb = e.Workbooks.Open("C:\Users\吴宇航\Desktop\新建文件夹\8月份日排产-成品.xls");
rgb =[25500];for i=1:66%数据寻找范围
for j=1:25%数据寻找范围(注意不能超过一开始读入的范围)
ifmod(i,2)==0&&(data(i,j)>0)
str2=int2str(i+1);%int to str行数当作字符
lie=lettertoxls(j+3);%列转换成ABC这样子
poistion=strcat(lie,str2);%字符串连接函数,[列,行],如A1单元格,这里只能用strcat拼接,若果用[]拼接,形成的是str类型,不适用于下文Range中参数类型,而strcat拼接出来的是cell。
ewb.Worksheets.Item(1).Range(poistion).Interior.Color = rgb*256.^(0:2)';%红色
end
end
end
ewb.Save;
ewb.Close(false);
e.Quit;
e.delete;