现在的位置: 首页 > 综合 > 正文

VBS 读取excel内容并写入到文本文件

2018年02月14日 ⁄ 综合 ⁄ 共 883字 ⁄ 字号 评论关闭
' we can pass the excel file name and worksheet as the parameter and using Wscript.Arguments to use it in the script

if(Wscript.Arguments.Count<3) then
 msgbox "The count of Arguments is "&Wscript.Arguments.Count
 'Quit VBS script
 'Wscript.Quit
End if


Set oExcel=CreateObject("excel.application")
Set oWorkBook=oExcel.Workbooks.Open("C:\Users\lude\Downloads\Hypo Case\Basic Property.xls")
'use the worksheet "Prop_Methods"
Set oSheet=oWorkBook.Sheets("Prop_Methods")

'Get the used range
'Set Sheet = oExcel.Worksheets("Prop_Methods").UsedRange

Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("c:\testfile.txt", True)
a.Close
' Read=1 Write=2 Append =8
Set a = fs.OpenTextFile("c:\testfile.txt", 8,false)


‘we can write more value to the text file by using loop

value=oSheet.cells(4,2)
a.WriteLine(value)
value=osheet.cells(5,2)
a.WriteLine(value)
value=osheet.cells(6,2)
a.WriteLine(value)
a.Close

Set oSheet=Nothing 
oExcel.Quit

现在可以根据自己的需要修改或扩展脚本实现更丰富的功能

抱歉!评论已关闭.