There are mainly 3 ways that we can access data from an excel file. Before using the below methods please create file calles Sample.xls in C:\ Drive
1. Data table
2. Using Excel Application COM methods
3. Using Database Connection methods.
1. Data Table :
Data Table is the quickest way to access data from any excel file and there are couple of methods provided by QTP. I am listing few of the methods.
1. datatable.import
2. datatable.importsheet
3. datatable.addsheet
4. datatable.getSheet
QTP has comprehensive help on the above topic
2. Excel Application COM methods
The second of accessing data from an excel file is by creating a Excel instance and with the help of that we can access the data of an excel.
Creating a excel file and sheet
Set xlObj= CreateObject("Excel.Application")
Set xlBook = xlObj.Workbooks.Add
set xlSheet = xlBook.worksheets.Add
xlObj.worksheets(4).name = "SampleSheet"
xlBook.SaveAs("C:\sample.xls")
set xlObj = nothing
set xlBook = nothing
set xlSheet = nothing
3. Excel application with Database connection
Const varStatic = 3
Const varLock = 3
Const varCmdText = &H0001
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")
objConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\sample.xls; Extended Properties=""Excel 8.0;HDR=Yes;"";"
objRecordset.Open "Select * FROM [Sheet1$]", objConnection, varStatic, varLock, varCmdText
Do Until objRecordset.EOF
print objRecordset.Fields.Item("ColumnName1")
objRecordset.MoveNext
Loop
Set objConnection = nothing
Set objRecordSet = nothing
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment