VBS操作文件 数据库

文本文件的读取
Option Explicit

Const ForReading = 1

Const ForWritting = 2

Const ForAppending = 8

Dim fso,file,msg

Set fso=CreateObject("Scripting.FileSystemObject")

Set file=fso.OpenTextFile("d:\a.txt",ForReading,True)

Do While Not file.AtEndOfStream

msg = file.ReadLine

MsgBox msg

Loop

file.Close

Set file=Nothing

Set fso=Nothing

第一步:首先创建两个对象,一个FileSystemObject对象fso,然后用fso对象的opentextfile方法去打开一个文件。并返回一个textStream对象给file。

第二步:使用file对象对打开的文件进行操作,例子中是对文件的读取。读取通过file对象的一个方法ReadLine来实现。

第三步:读取完毕,关闭文件

第四步:释放file对象和fso对象

File还有一些其它的方法,如Read、ReadAll等

文本文件的写入
Option Explicit

Const ForReading = 1

Const ForWritting = 2

Const ForAppending = 8

Dim fso,file,msg

Set fso=CreateObject("Scripting.FileSystemObject")

Set file=fso.OpenTextFile("d:\a.txt",ForWritting,True)

File.WriteLine “hello world”

File.WriteLine “this is a writing file case”

file.Close

Set file=Nothing

Set fso=Nothing

文件写入的步骤跟读取是一样的,唯一的区别是它们打开文件的方式不同。一个是以读的方式打开,另一个是以写的方式,这通过OpenTextFile方法的参数实现。OpenTextFile方法的第二个参数是一个常量值:1、2、8,1是ForReading的,2是ForWritting的,3是ForAppending的追加方式。

Excel文件的读写


Option Explicit

Dim xlApp,xlFile,xlSheet,xlSheet2

Dim iRowCount,iLoop,jLoop,numAdd(4),iColumnCount,record

Set xlApp=CreateObject("Excel.Application")

Set xlFile=xlApp.Workbooks.Open("D:\data.xls")

Set xlSheet=xlFile.Sheets("Sheet1")

Set xlSheet2=xlFile.Sheets("Sheet2")



iRowCount=xlSheet.usedRange.Rows.Count

iColumnCount=xlSheet.usedRange.Columns.Count



For iLoop = 1 To iRowCount

For jLoop = 1 To iColumnCount

numAdd(jLoop) = xlSheet.Cells(iLoop,jLoop)

'MsgBox numAdd(jLoop)

xlSheet2.Cells(iLoop,jLoop)=numAdd(jLoop)



'MsgBox xlSheet2.Cells(iLoop,jLoop)

Next

record = Join(numAdd," ")

MsgBox record

Next



xlFile.Save

xlFile.Close

xlApp.Quit

Set xlSheet = Nothing

Set xlSheet2 = Nothing

Set xlFile = Nothing

Set xlApp = Nothing

第一步:Excel的三层概念:Application、ExcelFile、Sheet,因此需要创建三个对象来实现对Excel文件的操作。首先创建一个Excel.Application对象xlApp,然后通过xlApp打开一个Excel文件,并返回一个对象给xlFile,最后通过xlFile的Sheets属性来操作某一个sheet。

第二步:通过sheet对象的cells子对象来操作某一sheet中的区域,sheet对象可以通过usedRange属性来识别此文件已经使用的cells。通过其中的count方法来获取具体值。

第三步:操作完成,保存文件、关闭文件、退出程序

第四步:释放对象

数据库的读写
Dim Cnn, Rst, strCnn

Set Cnn = CreateObject( "ADODB.Connection“ )

Set Rst = CreateObject( "ADODB.Recordset“ )

strCnn= "Provider=Microsoft.Jet.OLEDB.4.0.1;Data Source=C:\qtp_file\cal.mdb; Persist Security Info=False"

Cnn.Open strCnn

Rst.Open "Select * from student", Cnn

Rst.MoveFirst

Do While Not Rst.EOF

MsgBox Trim(Rst.Fields("name"))

Rst.MoveNext

Loop

Rst.Close

Cnn.Close

Set Rst = Nothing

Set Cnn = Nothing

1) 创建两个对象Connection对象和RecordSet对象和一个字符串连接串

2) 使用Connection通过连接串连接数据库

3) 将数据库操作的结果集保存到RecordSet对象中

4) 对于结果集中的数据可以通过RecordSet对象的方法来进行操作,Move系列方法是移动游标到相应位置,Fields属性来获取相应的字段值。

5) 关闭结果集、关闭数据库连接

6) 释放对象

其它的数据库文件的操作都是类似的,只要通过相应的字符串连接串进行连接。

xml文件的读写
Dim xmlDoc, xmlRoot, ChildItem, msg

Set xmlDoc = CreateObject ("Microsoft.XMLDOM")

xmlDoc.Load "C:\qtp_file\addressbook.xml" '使用load方法来加载xml文档,建立dom树和xml文档之间的关联



Set xmlRoot = xmlDoc.documentElement '获取XML文档的根元素节点



For Each ChildItem In xmlRoot.childNodes

MsgBox "当前节点是:" & ChildItem.baseName '显示当前节点的名称

Set node1=ChildItem.childNodes.item(0)

Set node2=ChildItem.childNodes.item(1)

Set node3=ChildItem.childNodes.item(2)

MsgBox node1.baseName

MsgBox node2.baseName

MsgBox node3.baseName

MsgBox node1.firstChild.nodeValue

MsgBox node2.firstChild.nodeValue

MsgBox node3.firstChild.nodeValue

Next

1) 同样通过创建XmlDom对象,使用对象通过load方法来关联xml文件

2) 创建xmlRoot对象来获取xml文件的根元素

3) 通过对xmlRoot的子节点来操作整个文件

4) 当然,最后结束操作要释放对象


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/w7822055/archive/2009/05/18/4197785.aspx
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值