Datatable: Creation, Writing and Reading the values from Excel Sheet
Hi Friends,
I am practicing QTP and currently working with Datatable.
Please help me in writing a descriptive program for storing the information in excel sheet for the following scenarios:
Scenarios:
1) Creation of excel sheet (at run time).
2) Storing the datas (user name and password) into the excel sheet by creating it (at run time).
3) Reading the datas (user name and password) from the above said excel sheet (at run time).
I tried, but I was not successful for all the three.
I am waiting for your replies.....
Re: Datatable: Creation, Writing and Reading the values from Excel Sheet
Hi,
''''Creation of Excel Sheet'''''''''''
Set Excel=createObject("Excel.Application")
Set ExcelSheet=createObject("Excel.sheet")
ExcelSheet.Application.visible=true
'''''storing the datas into excel sheet'''''''''''
Excel.ActiveSheet.cells(1,1).value="UserName"
Excel.ActiveSheet.cells(1,2).value="Password"
Excel.ActiveSheet.cells(2,1).value="arunsingh"
Excel.ActiveSheet.cells(2,2).value="rajaputra"
ExcelSheet.SaveAs "C:/text1.xls"
datatable.Import "C:/text1.xls"
'''''Reading the datas from excel sheet'''''''
msgbox "UserName is= "+datatable(1,1)
msgbox "PassWord is= "+datatable(2,1)
____________________
Arunsingh Rajaputra
Re: Datatable: Creation, Writing and Reading the values from Excel Sheet
[QUOTE]'''''Reading the datas from excel sheet'''''''
msgbox "UserName is= "+datatable(1,1)
msgbox "PassWord is= "+datatable(2,1)[/QUOTE]
Rajaputra, this code will only read data from QTP's DataTable, not an Excel Sheet.
This is a very simple code, but it works. You can read single cell data using this code but this is not a very good alternative if you have lots of data in your Excel Sheet.
[COLOR="DimGray"]Function ReadExcel (nRow, nCol)
Set oExcel = CreateObject("Excel.Application")
Set oWorkBook = oExcel.Workbooks.Open("C:\testFile.xls")
Set oSheet = oWorkBook.Activesheet
strVal = oSheet.Cells (nRow, nCol).Value
End Function[/COLOR]
Re: Datatable: Creation, Writing and Reading the values from Excel Sheet
Anshoo,
Yes, The last 2 lines in my code is reading data from datatable.
You can use below line to read the data from excell sheet......
msgbox "UserName is= "+Excel.ActiveSheet.cells(2,1)
msgbox "Password is= "+Excel.ActiveSheet.cells(2,2)
_____________
ArunSingh Rajaputra
Re: Datatable: Creation, Writing and Reading the values from Excel Sheet
Hi rajaputra,
i've tried the same code for writing into excel sheet. at first it worked. But, now it is not working. it is giving this error message.
object required: 'Excel.ActiveSheet'
Can you please help me what do i have to do now.
[QUOTE=rajaputra;26869]Hi,
''''Creation of Excel Sheet'''''''''''
Set Excel=createObject("Excel.Application")
Set ExcelSheet=createObject("Excel.sheet")
ExcelSheet.Application.visible=true
'''''storing the datas into excel sheet'''''''''''
Excel.ActiveSheet.cells(1,1).value="UserName"
Excel.ActiveSheet.cells(1,2).value="Password"
Excel.ActiveSheet.cells(2,1).value="arunsingh"
Excel.ActiveSheet.cells(2,2).value="rajaputra"
ExcelSheet.SaveAs "C:/text1.xls"
datatable.Import "C:/text1.xls"
'''''Reading the datas from excel sheet'''''''
msgbox "UserName is= "+datatable(1,1)
msgbox "PassWord is= "+datatable(2,1)
____________________
Arunsingh Rajaputra[/QUOTE]
Re: Datatable: Creation, Writing and Reading the values from Excel Sheet
hai jyothipola,
Will you please post your code what you have tried here.
Thanks
Deepa