每天进步一点点!
VBScript中的文件读写
上一篇 /
下一篇 2010-04-27 11:12:41
/ 个人分类:VBS基础知识
•文本文件的读写代码示例Option ExplicitConst ForReading= 1,ForWriting = 2,ForAppending = 8Dim fso,file,msgSet fso= CreateObject("Scripting.FileSystemObject")Set file = fso.OpenTextFile("c:\calc.txt",ForReading)While (Not file.AtEndOfLine)msg= msg& file.ReadLine& Chr(13) & Chr(10)WendMsgBoxmsgfile.CloseSet file = NothingSet fso= Nothing•Excel文件的读写代码示例Dim xlApp,xlWorkBook,xlSheetDim iRowCount,iLoop,numAddSet xlApp= CreateObject("Excel.Application")xlApp.Visible= TrueSet xlWorkBook= xlApp.Workbooks.Open("c:\data.xls")Set xlSheet= xlWorkBook.Sheets("Sheet1")iRowCount= xlSheet.usedRange.Rows.CountFor iLoop= 2 To iRowCountnumAdd= xlSheet.Cells(iLoop,1)NextxlWorkBook.SavexlWorkBook.ClosexlApp.QuitSet xlSheet= NothingSet xlWorkBook= NothingSet xlApp= Nothing数据库文件的读写代码示例Dim Cnn,Rst,strCnnstrCnn= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\calc.mdb;Persist Security Info=False“Set Cnn= CreateObject("ADODB.Connection")Cnn.OpenstrCnnSet Rst= CreateObject("ADODB.Recordset")Rst.Open"Select * from calc",CnnRst.MoveFirstDo While Not Rst.EOFMsgBoxTrim(Rst.Fields("TestResult"))Rst.MoveNextLoopRst.CloseCnn.CloseSet Rst= NothingSet Cnn= NothingXML文件的读写代码示例Dim xmlDoc, xmlRoot, rootChildItem, msgSet xmlDoc= CreateObject("Microsoft.XMLDOM")xmlDoc.async= FalsexmlDoc.Load"C:\calc.xml"If xmlDoc.parseError.errorCode<> 0 ThenMsgBox"XML loaded failed. The reason is :" & xmlDoc.parseError.reasonExit SubEnd IfSet xmlRoot= xmlDoc.documentElementIf Not xmlRoot.hasChildNodesThen Exit SubFor Each rootChildItemIn xmlRoot.childNodesIf rootChildItem.nodeName= "TestCase" Thenmsg= msg& rootChildItem.firstchild.nodeValue& vbNewLineEnd IfNextMsgBoxmsg
收藏
举报
TAG: