每天进步一点点!

VBScript中的文件读写

上一篇 / 下一篇  2010-04-27 11:12:41 / 个人分类:VBS基础知识

•文本文件的读写
代码示例
Option Explicit
Const ForReading= 1,ForWriting = 2,ForAppending = 8
Dim fso,file,msg
Set fso= CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("c:\calc.txt",ForReading)
While (Not file.AtEndOfLine)
msg= msg& file.ReadLine& Chr(13) & Chr(10)
Wend
MsgBoxmsg
file.Close
Set file = Nothing
Set fso= Nothing

•Excel文件的读写
代码示例
Dim xlApp,xlWorkBook,xlSheet
Dim iRowCount,iLoop,numAdd
Set xlApp= CreateObject("Excel.Application")
xlApp.Visible= True
Set xlWorkBook= xlApp.Workbooks.Open("c:\data.xls")
Set xlSheet= xlWorkBook.Sheets("Sheet1")
iRowCount= xlSheet.usedRange.Rows.Count
For iLoop= 2 To iRowCount
numAdd= xlSheet.Cells(iLoop,1)
Next
xlWorkBook.Save
xlWorkBook.Close
xlApp.Quit
Set xlSheet= Nothing
Set xlWorkBook= Nothing
Set xlApp= Nothing
数据库文件的读写
代码示例
Dim Cnn,Rst,strCnn
strCnn= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\calc.mdb;Persist Security Info=False“
Set Cnn= CreateObject("ADODB.Connection")
Cnn.OpenstrCnn
Set Rst= CreateObject("ADODB.Recordset")
Rst.Open"Select * from calc",Cnn
Rst.MoveFirst
Do While Not Rst.EOF
MsgBoxTrim(Rst.Fields("TestResult"))
Rst.MoveNext
Loop
Rst.Close
Cnn.Close
Set Rst= Nothing
Set Cnn= Nothing
XML文件的读写
代码示例
Dim xmlDoc, xmlRoot, rootChildItem, msg
Set xmlDoc= CreateObject("Microsoft.XMLDOM")
xmlDoc.async= False
xmlDoc.Load"C:\calc.xml"
If xmlDoc.parseError.errorCode<> 0 Then
MsgBox"XML loaded failed. The reason is :" & xmlDoc.parseError.reason
Exit Sub
End If
Set xmlRoot= xmlDoc.documentElement
If Not xmlRoot.hasChildNodesThen Exit Sub
For Each rootChildItemIn xmlRoot.childNodes
If rootChildItem.nodeName= "TestCase" Then
msg= msg& rootChildItem.firstchild.nodeValue& vbNewLine
End If
Next
MsgBoxmsg


TAG:

 

评分:0

我来说两句

日历

« 2024-04-16  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 11356
  • 日志数: 27
  • 图片数: 1
  • 建立时间: 2009-01-05
  • 更新时间: 2010-07-09

RSS订阅

Open Toolbar