先在A1单元格设置【格式->单元显示格式】中的“日期->2004-6-16”,脚本中去掉字符串中空格的操作,主要是针对手动输入内容时可能产生的空格,如果是读取数据库中日期类型的数据,就可以不用判断空格。
dim date '整理后的日期字符串
dim newdate '待输出的日期字符串
value=chinaexcel.getcellshowvalue(1,1) '获取单元格中的日期字符串
date=""
for i=1 to len(value)
if mid(value,i,1)<>" " then '去掉字符串中的空格,方便后面根据“-”的位置判断月份和日期
date=date + mid(value,i,1)
end if
next
newdate=""
for i=1 to len(date)
if i=6 and mid(date,8,1)="-" then '第8个字符是“-”,表示月份是2位数
newdate=newdate + "拾"
elseif i=len(date)-1 and mid(date,len(date)-2,1)="-" then '倒数第3个字符是“-”,表示日期是2位数
if mid(date,i,1)="1" then
newdate=newdate + "拾"
elseif mid(date,i,1)="2" then
newdate=newdate + "贰拾"
elseif mid(date,i,1)="3" then
newdate=newdate + "叁拾"
end if
elseif mid(date,i,1)="-" then
newdate=newdate + " "
else
j=mid(date,i,1)
if j="0" then
if i<5 then newdate=newdate + "零" '只有年份中的“0”才显示“零”
end if
if j="1" then newdate=newdate + "壹"
if j="2" then newdate=newdate + "贰"
if j="3" then newdate=newdate + "叁"
if j="4" then newdate=newdate + "肆"
if j="5" then newdate=newdate + "伍"
if j="6" then newdate=newdate + "陆"
if j="7" then newdate=newdate + "柒"
if j="8" then newdate=newdate + "捌"
if j="9" then newdate=newdate + "玖"
end if
next
chinaexcel.setcellvalue 2,2,newdate
chinaexcel.refresh '设置单元格内容后需刷新报表