Get data from an Excel file without importing the data to the data table

来源:互联网 发布:淘宝的云客服 编辑:程序博客网 时间:2024/05/23 11:36

Create an Excel object using VBScript

The function below will allow to you to extract data from an external Excel file (specifying the sheet) without importing the file to the QuickTest data table. The function uses automation objects to create an Excel object.

GetValueInFile (sfilePath, isheet, irow, icolumn)

sfilePathThe path and name of the excel file.isheetThe sheet of the excel file. This can be an index number or the sheet name.irowThe row number in the table.icolumnThe column number in the table.

Function GetValueInFile (sfilePath, isheet, irow, icolumn)
   Set ExcelObj = CreateObject("Excel.Application")
   ExcelObj.Workbooks.Open sfilePath
   Set NewSheet = ExcelObj.Sheets.Item(isheet)
   value = NewSheet.Cells(irow,icolumn)
   ExcelObj.Application.Quit
   Set ExcelObj = Nothing
   GetValueInFile = value
End Function

Example:
dim filename
filename = "D:\temp\Test.xls"

msgbox GetValueInFile(filename,1,2,2)
msgbox GetValueInFile(filename,"Sheet1",1,1)
msgbox GetValueInFile(filename,2,2,2)
msgbox GetValueInFile(filename,3,1,1)


0 0
原创粉丝点击