从楼顶的问题来看,固然我二楼的代码是没有问题的,但在随风在测试中又遇到很多问题:如前三行因单元格
合并导致运行出错;读取形如0.*的数据时出现超长数据;要求对数值取两位小数并四舍五入,于是,修改代码
如下:- '另存工作表为txt文件,并将空值替换为#
- dim hang,lie,counter,counter1,vbstr '声明变量
- on error resume next '忽略错误
-
- '设置对象
- set wshshell=createobject ("wscript.shell")
- path=wshshell.currentdirectory '当前路径
- set objexcel=createobject ("excel.application")
- set objworks=objexcel.workbooks.open (path&"\"&wscript.arguments(0))
- set objfso=createobject("scripting.filesystemobject")
- set objtext=objfso.opentextfile(path&"\"&wscript.arguments(1),2,true)
- objexcel.worksheets(1).activate
-
- '从第四行开始获取工作表有效总行列数(舍弃前三行非数据行)
- counter=0
- do
- counter=counter+1
- loop until objexcel.cells(4,counter).value=""
- lie=counter-1:counter=3
- do
- counter=counter+1
- loop until objexcel.cells(counter,1).value=""
- hang=counter-1:counter=0
-
- '读取工作表数据并将空值替换为#
- for counter=4 to hang
- for counter1=1 to lie
- vbstr=objexcel.cells(counter,counter1).value
- if vbstr="" then
- strexcel=strexcel&" #"
- else
- if replace(vbstr,".","")<>vbstr then vbstr=round(vbstr,2) '对小数取两位小数并四舍五入
- if mid(vbstr,1,1)="." then vbstr="0"&vbstr
- strexcel=strexcel&" "&vbstr
- end if
- next
- strexcel=strexcel&vbcrlf
- next
- objtext.write strexcel
- objtext.close
- objworks.close
-
- '清空对象,释放内存
- set objtext=nothing
- set objfso=nothing
- set objworks=nothing
- set objexcel=nothing
- set wshshell=nothing
复制代码
[ 本帖最后由 batman 于 2010-1-5 17:36 编辑 ] |