Geeks Talk

Prepare for your Next Interview


Welcome to the Geeks Talk forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

reading data from excel sheet in QTP

This is a discussion on reading data from excel sheet in QTP within the QTP forums, part of the Software Testing category; How to retrieve data from excel sheet by checking column name and its value & next column and its value(without using keyword view) scripting only This is the question asked ...

Go Back   Geeks Talk > Software Testing > QTP
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 12-04-2006
Expert Member
 
Join Date: Oct 2006
Posts: 689
Thanks: 0
Thanked 53 Times in 44 Posts
JobHelper is on a distinguished road
reading data from excel sheet in QTP

How to retrieve data from excel sheet by checking column name and its value & next column and its value(without using keyword view) scripting only

This is the question asked by visitor Nags
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-11-2006
Junior Member
 
Join Date: Dec 2006
Location: India
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
mbazith is on a distinguished road
Re: reading data from excel sheet in QTP

Hi,

Import the excel sheet to your datatable. Then use a for loop like
row_cnt=datatable.getrowcount
for i=1 to row_cnt
datatable.getsheet(i)
a=datatable.value("header",1)
b=datatable.value("header1",1)
.
.
datatable.setnextrow
Next
Reply With Quote
  #3 (permalink)  
Old 12-12-2006
Junior Member
 
Join Date: Dec 2006
Posts: 2
Thanks: 0
Thanked 1 Time in 1 Post
code_x is on a distinguished road
Re: reading data from excel sheet in QTP

You can simply access the excell sheet (not import) by using the script. See bellow example:

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.WorkBooks.Open("path of yourfile.xls")
Set objDriverSheet = objWorkbook.Worksheets("name of the sheet")

then just acces the cell itself :
row = 1
objDriverSheet.Cells(row,"A").Value

I used just default column names by A,B,C,......

Hope that helps,
Reply With Quote
  #4 (permalink)  
Old 09-03-2007
Junior Member
 
Join Date: Sep 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
raj2k0072000 is on a distinguished road
Re: reading data from excel sheet in QTP

I am using the For Loop to get data from the data table. The summary code is:
C=datatable.getrowcount
For i=1 to c
msgbox Datatable.Value("Column1")
Next
'The message box always returns data of row 1. Irrespective of the iteration. How is it possible.
Reply With Quote
  #5 (permalink)  
Old 09-11-2007
Contributing Member
 
Join Date: Feb 2006
Posts: 85
Thanks: 0
Thanked 3 Times in 3 Posts
kunals is on a distinguished road
Re: reading data from excel sheet in QTP

the reason may be cos you r not updating the current row value :
May be this will give you some idea:

i=1
RowCount = DataTable.GetSheet("Global").GetRowCount
CurrentRow = DataTable.GetSheet("Global").GetCurrentRow
For j =1 to RowCount
...
Do whatever you want to do with that value here
...
DataTable.SetCurrentRow (i) 'set the current row to the next one
Next


Thanks,
Kunal.
Reply With Quote
The Following User Says Thank You to kunals For This Useful Post:
  #6 (permalink)  
Old 02-16-2009
Junior Member
 
Join Date: Feb 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
tguruk is on a distinguished road
Re: reading data from excel sheet in QTP

Please check the below script for reading/Accessing data from excell sheet.

n = DataTable.getSheet("Action1").GetRowCount

For i = 1 to n dataTable.set currentrow(i)

x = dataTable.value("data", 1)
if x = "1" Then
msgbox x
else
msgbox " not a valid data"
end if

Next.
Reply With Quote
  #7 (permalink)  
Old 02-16-2009
Junior Member
 
Join Date: Feb 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
tguruk is on a distinguished road
Re: reading data from excel sheet in QTP

Please check the below script for reading/Accessing data from excell sheet.

n = DataTable.getSheet("Action1").GetRowCount

For i = 1 to n dataTable.set currentrow(i)

x = dataTable.value("data", 1)
if x = "1" Then
msgbox x
else
msgbox " not a valid data"
end if

Next.
Reply With Quote
  #8 (permalink)  
Old 02-16-2009
Junior Member
 
Join Date: Aug 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
aalurip is on a distinguished road
Wink Re: reading data from excel sheet in QTP

Thansk for usefull info
Reply With Quote
  #9 (permalink)  
Old 02-16-2009
Expert Member
 
Join Date: Oct 2007
Location: hyd-India
Posts: 122
Thanks: 3
Thanked 17 Times in 15 Posts
ch.bhaskar.mca is on a distinguished road
Re: reading data from excel sheet in QTP

here we can use two ways of coding.
1st method:
datatable.importsheet "path of the excel file.xls",source sheetID,desination sheetID
n = datatable.getsheet("desination sheetname").getrowcount
for i = 1 to n
columnname = datatable.getsheet("destination sheetname").getparameter(i).name
if colunmname = knowncolumnname then
value = datatable.getsheet(destinationsheetname).getparameter(i)
end if
next

2nd method:
set objexcel = createobject("excel.application")
Set objWorkbook = objExcel.WorkBooks.Open("path of the file.xls")
Set objDriverSheet = objWorkbook.Worksheets("name of the sheet")
columncount = objDriverSheet.usedrange.columns.count
rowcount = objDriverSheet.usedrange.rows.count
for i = 1 to colunmcount
columnname = objDriversheet.cells(i,1)
if columnname = knowncolumnname then
for j = 1 to rowcount
fieldvalue = objdriversheet.cells(j,i)
next
end if
next

Thanks
Bhaskar kumar

Reply With Quote
  #10 (permalink)  
Old 03-23-2009
Junior Member
 
Join Date: Mar 2009
Location: Charlotte, nc
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
QTP_Auto is on a distinguished road
Re: reading data from excel sheet in QTP

Hello Bhaskar,

I used your 2nd method to read data from excel sheet. Can you please tell me how can I create an array to continously loop through excel sheet and read ONLY required values.

Shouldn't I use an array inside the for loop or outside? Please help.

Thanks much.
Reply With Quote
  #11 (permalink)  
Old 03-23-2009
Junior Member
 
Join Date: Mar 2009
Location: Charlotte, nc
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
QTP_Auto is on a distinguished road
Re: reading data from excel sheet in QTP

Hi Bhaskar,
I used your 2nd method, but I'm not able to get the column count and row count of the used range of the excel sheet. Could you please help. I'll really appreciate it. Thanks.

Below is my code:
Set objExcelObj = CreateObject("Excel.Application")
objExcelObj.Visible = True
Set objWorkbook = objExcelObj.Workbooks.Open(strParticipantDataSetupFile)

Set objParticipantSheet = objWorkbook.Worksheets(strSheetName)
columncount = objParticipantSheet.usedrange.columns.count
rowcount = objParticipantSheet.usedrange.rows.count

For i = 1 to columncount
columnname = objParticipantSheet.cells(i,1)
If columnname = "PlatformName_vc" Then
For j = 1 to rowcount
fieldvalue = objParticipantSheet.cells(j,i)
Next
End If
Next
Reply With Quote
  #12 (permalink)  
Old 03-25-2009
Junior Member
 
Join Date: Mar 2009
Location: india
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
archgk is on a distinguished road
Re: reading data from excel sheet in QTP

Hi ,
I have imported the file & tried with your code.
Suppose there are 3 records in my file & I want to retrieve the values from column 'Name'.
But I am getting the required output 3 times since there are 3 records. I want the required O/P only once. Could u pls guide me on this? Below is my code.

DataTable.Import "C:\1.xls"
row_cnt=datatable.getrowcount
for i=1 to row_cnt
datatable.getsheet(1)
a=datatable.value("DbTable_Row_1_Col_1_out",1)
'b=datatable.value("header1",1)

msgbox a
datatable.setnextrow
Next

Last edited by archgk; 03-25-2009 at 05:31 AM.
Reply With Quote
  #13 (permalink)  
Old 3 Weeks Ago
Junior Member
 
Join Date: Oct 2009
Location: India
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Anjulika Srivastava is on a distinguished road
Re: reading data from excel sheet in QTP

Hi All,

Just try below code , hope this will help you

''opens excel file
Set xlApp = GetObject("","Excel.Application")
xlApp.visible = true
Set xlWrkbk = xlApp.Workbooks.Open("abc.xls")
Set xlWrksht = xlWrkbk.Worksheets("Data") ' Data is the name of the sheet
intStartRow = 2 ' Row from whcih you need to start

For intRow = intStartRow to xlWrksht.UsedRange.Rows.Count

strAccountNumber = Trim(xlWrksht.Range("A" & intRow))
strAccuntName = Trim(xlWrksht.Range("B" & intRow))
dtDate = Trim(xlWrksht.Range("C" & intRow))

Next
Reply With Quote
Reply

  Geeks Talk > Software Testing > QTP

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
data conversion jaindharmendrakumar SQL 0 11-21-2006 05:57 AM
Tips on Reading Textbooks admin Career Advice 0 10-16-2006 08:56 AM
Data type in SQL blenda Oracle 2 06-29-2006 02:16 PM
Configuring Data Source Lokesh M ASP.NET 0 06-28-2006 04:24 AM
Data Retrieval in UNIX blenda Unix/Linux 1 06-24-2006 10:17 PM


All times are GMT -4. The time now is 06:41 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved