您的位置:首页 > 编程语言 > Qt开发

【QTP】写入txt文件的操作

2012-10-23 13:24 253 查看
在QTP中,经常会遇到需要写入外部文件的地方,比如写Log什么的,这时,可以使用下面代码进行写Txt操作。

'新建文件
Dim FSO
Const ForReading=1,ForWriting=2,ForAppending=8         '参数赋值(1:只读,2:只写,3:追加)
Set FSO = CreateObject("Scripting.FileSystemObject")   '创建一个文本对象

Dim txtPath
txtPath = "D:\log.txt"
FSO.OpenTextFile txtPath,8,true            'true表示如果当前目录下不存在1.txt文件则创建一个。

'写文件
Call QTP_Writetxt(txtPath,"我是追加")
Call QTP_Writetxt2(txtPath,"我是改写")


然后是两个函数:

'===========================================
'写文件函数(追加)
'===========================================
Public Function QTP_Writetxt(oPath,words)
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
Set logFile = FSO.OpenTextFile(oPath, 8, true)

logFile.WriteLine (CStr(words))
logFile.Close

Set logFile = Nothing
Set FSO = Nothing
End Function

'===========================================
'写文件函数(改写)
'===========================================
Public Function QTP_Writetxt2(oPath,words)
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
Set logFile = FSO.OpenTextFile(oPath, 2, true)

logFile.WriteLine (CStr(words))
logFile.Close

Set logFile = Nothing
Set FSO = Nothing
End Function


除了直接打印之外,我还会加上写html语句,然后新建文件的后缀名也改成.html,这样,保存的文件就是一个网页啦,可以写入超链接,颜色,插入图片等等一系列动作。

这样,一个打印就可以做到图文并茂啦~

下面的图是我的用法,用了下QTP报告自带的CSS,大概如下

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: