Importing test data from csv file to QTP Datatable.

1,690 views
Skip to first unread message

Manu

unread,
Aug 6, 2012, 10:32:04 AM8/6/12
to qtp-ele...@googlegroups.com
Hi,
 
Can anyone guide on importing test data from a csv file to QTP data table and exporting the updated QTP datatable back to a csv file format.
 
Regards,
Manu

Manu

unread,
Aug 6, 2012, 11:15:35 AM8/6/12
to qtp-ele...@googlegroups.com
I am able to import using below code, but the values are appearing all in the frst column for each row
 
Test_Data = "C:\temp\Tests\Test_Steps.csv"
DataTable.Import(Test_Data)
 
is there anyway i can get the values in respective columns.

Tarun Jain

unread,
Aug 6, 2012, 11:46:48 AM8/6/12
to Manu, qtp-ele...@googlegroups.com
Manu,

Reason is that the Excel reads the csv as comma separated file and if there is not delimiter in your file everything reads in the first column.

And Datatabel only works with Excel files.

Please try to change the csv file to excel first manually and then try to play with that.

Thanks,
--
Thanks & Regards

TARUN JAIN
Mobile:- 9717229000

Manu

unread,
Aug 7, 2012, 8:00:42 AM8/7/12
to qtp-ele...@googlegroups.com, Manu
Thanks Tarun.
 
 
In my project i can't use excel or any other application apart from QTP in the workstation where AUT installed. So compelled to use CSV File.
 
I could manage to get some deteails through googling which i am sharing. The below function will help to import a CSV file to data table column wise Where we can pass destination sheet, and delimiter.
 
Option Explicit

Function ImportCSVFileDatatable(CSVFilePath, SheetName, HeaderDelimiter)
Dim vFilePath
Dim vFSO
Dim vF
Dim vFData
Dim varrData
Dim vCSVValue
Dim vCSVSheet
Dim vCSVFirstLine
Dim vCSVColumn
Dim vColumnIndex
Dim vRIndex
Dim vCIndex
vFilePath = CSVFilePath ' Specifying the filepath
'Open CSV File using file system object
Set vFSO = createObject("scripting.filesystemobject")
Set vF = vFSO.OpenTextFile(vFilePath)
vCSVFirstLine = vF.ReadLine  ' Treating like first line is the column name.
vCSVColumn = split(vCSVFirstLine, HeaderDelimiter) 'Split the line using header delimiter
Set vCSVSheet =  Datatable.GetSheet(SheetName)  ' Get the specified sheet
'Add the splitted values as data table columns
For vColumnIndex = lbound(vCSVColumn) to ubound(vCSVColumn)
 vCSVSheet.AddParameter vCSVColumn(vColumnIndex),""
Next
While Not vF.AtEndOfStream
 vRIndex = vF.Line - 1 'Specify the row index
 vFData = vF.ReadLine 'Read CSVFile Line
 varrData = split (vFData,",") ' Split the line.
 vCIndex = 1'Specify column index
 vCSVSheet.SetCurrentRow(vRIndex) ' Set row in datatable

 'Add values in datatable
 For Each vCSVValue in varrData
  vCSVSheet.GetParameter(vCIndex).Value = vCSVValue
  vCIndex = vCIndex + 1
 Next
Wend
vF.Close
Set vFSO = Nothing
End Function
 
'Calling the function
ImportCSVFileDatatable "C:\Users\Manu\Documents\QTP WorkSpace\Test_Steps.csv", "Action1",","

Tarun Jain

unread,
Aug 7, 2012, 8:10:53 AM8/7/12
to Manu, QTP eLearn Team
Thanks for the information Manu.

Thanks,
Reply all
Reply to author
Forward
0 new messages