Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Import Excel Sheet
#1
Solved: 10 Years, 8 Months, 4 Weeks ago
Hi
I am trying to create a function to dynamically add sheet and import data in the runtime datatable in qtp.The function is adding all sheets but not retrieving data from excel .Codes are below.Can anyone resolve the problem ?


Code:
importdata("C:\Book1.xls")

Function importdata(filepath)
Set xlapp = createobject("Excel.Application")
Set xlbook = xlapp.Workbooks.Open(filepath)
allsheets = xlapp.Worksheets.Count
For i = 1 to allsheets
    sheetname = xlapp.Worksheets(i).Name
    datatable.AddSheet sheetname
    datatable.ImportSheet filepath,sheetname,sheetname

Next
    xlbook.Close
    xlapp.Quit

Set xlapp = nothing
Set xlbook = nothing

End Function
Reply
#2
Solved: 10 Years, 8 Months, 4 Weeks ago
Hi,

Just by adding a line of code we can achieve your goal.
Code:
importdata("C:\Book1.xls")

Function importdata(filepath)
Set xlapp = createobject("Excel.Application")
Set xlbook = xlapp.Workbooks.Open(filepath)
allsheets = xlapp.Worksheets.Count
For i = 1 to allsheets
sheetname = xlapp.Worksheets(i).Name
datatable.AddSheet sheetname
datatable.ImportSheet filepath,sheetname,sheetname

Next
xlbook.Close
xlapp.Quit

Set xlapp = nothing
Set xlbook = nothing
'need this line of code, the possible reason is as you are using the excel object alredy the file is in use so it could not load the data (may be I am not sure) but by using the below LOC you can import all the sheets.
DataTable.Import filepath
End Function
Let me know if you need any info.
Thanks,
SUpputuri
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Reading data from excel sheet serenediva 1 8,776 03-03-2017, 10:07 AM
Last Post: vinod123
  How import final calculated values by cell formula from Excel not the formula itself. qtped 1 4,698 01-17-2017, 04:05 PM
Last Post: sagar.raythatha
Smile Importing Excel sheet - Reference Values are not recognized michiusa69 3 7,576 01-23-2015, 02:36 AM
Last Post: lkng2001
  How to pass a date from Excel sheet to web form revati.shendure 1 2,813 11-27-2014, 11:54 AM
Last Post: revati.shendure
  Datatable import error Prashanth 7 16,016 11-19-2014, 06:53 PM
Last Post: praveen.g3

Forum Jump:


Users browsing this thread: 1 Guest(s)