发新话题
打印

用脚本解决支票套打大写日期的问题

用脚本解决支票套打大写日期的问题

dim cOldDate '原始日期
dim cOoldDate'整理原始日期
dim cNewDate '新日期
dim cSingC '截取的单个字符
dim nNum '循环次数
cOoldDate=chinaexcel.GetCellShowValue(9,2)
if cOoldDate="" then
    cOoldDate=now()
    chinaexcel.setcellvalue 9,2,now()
end if '如果当前单元格无日期,则用系统当前日期替换
cOldDate=""
for nNum=1 to len(cOoldDate)
   cSingC=mid(cOoldDate,nNum,1)
   if cSingC<>" " then
     cOldDate=cOldDate+mid(cOoldDate,nNum,1)
   end if
next
cNewDate=""
cSingC=""
nNum=1
for nNum=1 to 10
  cSingC=MID(cOldDate,nNum,1)
  select case cSingC
    case "0"
      if nNum<>6 and nNum<>8 then
         cNewDate=cNewDate+"零"
      end if
    case "1"
      cNewDate=cNewDate+"壹"
    case "2"
      cNewDate=cNewDate+"贰"
    case "3"
      cNewDate=cNewDate+"叁"
    case "4"
      cNewDate=cNewDate+"肆"
    case "5"
      cNewDate=cNewDate+"伍"
    case "6"
      cNewDate=cNewDate+"陆"
    case "7"
      cNewDate=cNewDate+"柒"
    case "8"
      cNewDate=cNewDate+"捌"
    case "9"
      cNewDate=cNewDate+"玖"
  end select
  if nNum=4 then
     if cint(mid(cOldDate,5,2))>10 then
        cNewDate=cNewDate+"    "
     else
         cNewDate=cNewDate+"      "
     end if
  end if
  if nNum=5 and cSingC<>"0" then
    cNewDate=cNewDate+"拾"
  end if
  if nNum=6 then
     if cint(mid(cOldDate,7,2))>10 then
        if mid(cOldDate,8,1)<>"0" then
           cNewDate=cNewDate+"   "
        else
           cNewDate=cNewDate+"     "
        end if   
     else
         cNewDate=cNewDate+"     "
     end if
  end if
  if nNum=7 and cSingC<>"0" then
    cNewDate=cNewDate+"拾"
  end if
next
chinaexcel.setcellvalue 2,6,cNewDate

写的不好,望指教……

TOP

先在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  '设置单元格内容后需刷新报表

TOP

二楼的代码不错,但存在两个问题:
一是与银行支票日期书写规范要求不完全相符,银行支票书写要求如下:
    2009-1-1  :贰零零玖   零壹  零壹
    2009-10-21  :贰零零玖 壹拾 贰拾壹
二是不同日期转换后的长度不一样,需要使用空格来调整,以保证不管日期如何变化,套打位置始终正确

TOP

关于第一个问题,  2009-1-1  :贰零零玖   零壹  零壹,可以在
if i=6 and mid(date,8,1)="-" then  '第8个字符是“-”,表示月份是2位数
    newdate=newdate + "拾"
判断,是加“壹拾”还是“零壹”,日期那也是一样。
第二个问题,空格可以自己调整,在
elseif mid(date,i,1)="-" then
    newdate=newdate + "  "
这里是把年月之间、月日之间的空格一起调整了,你可以分开判断,自行调整。

[ 本帖最后由 tcf001 于 2009-12-31 18:02 编辑 ]

TOP

发新话题
最近访问的版块