Welcome 微信登录

首页 / 脚本样式 / JavaScript / 使用Vbscript操作Excel的示例代码

使用Vbscript操作Excel的示例代码2013-10-12 本站 cat我们可以用Vbscript快速的批处理一些Excel文件。Vbscript脚本需要保存为.vbs文件,双击运行,非常方便。

以下为示例代码,请保存为 run.vbs 文件:

" on error resume next

" define params:

im ExcelApp

im objWorkBook

im objImportSheet

im path

path = "C:/Users/Administrator/Desktop"

" open excel:

" From http://www.bianceng.cn

Set ExcelApp = CreateObject("Excel.Application")

ExcelApp.Visible = False

Set objWorkBook = ExcelApp.Workbooks.Open(path & "/1.xls")

Set xlsSheet = objWorkBook.Sheets("Sheet1") "页数名称

" read datas:

msgbox "第一行第一列数值: " & xlsSheet.Cells(1,1).Value

msgbox "第二行第一列数值: " & xlsSheet.Cells(2,1).Value

msgbox "第二行第二列数值: " & xlsSheet.Cells(2,2).Value

" set cells data:

xlsSheet.Cells(3,1).Value="插入的数据..."

msgbox "第三行第一列数值: " & xlsSheet.Cells(3,1).Value

" save

objWorkBook.SaveAs path & "/new.xls"

"objWorkBook.Save

" close

objWorkBook.Close

" clean & quit:

ExcelApp.Quit

Set objWorkBook = Nothing

Set objImportSheet = Nothing

Set ExcelApp = Nothing