【问题标题】:Cannot save webtable contents to excel file in qtp无法将 webtable 内容保存到 qtp 中的 excel 文件
【发布时间】:2016-06-28 14:00:55
【问题描述】:

我的代码无法将数据保存到现有的 Excel 文件。我可以从本地窗口看到它正在将数据从网络表复制到 Excel 工作表,但无法保存详细信息。如果我在这里遗漏任何错误,请您更正

   path = "D:\Demo\TestData\Shopping_Cart.xls"

set xl= CreateObject("excel.application")
xl.workbooks.open(path)
set nsheet=xl.sheets.item(1)

Set BrwsrCheckOut= Browser("name:=Checkout","title:=Checkout - Internet Explorer").page("title:=Checkout","name:=.*")
Set DesPrdChcKOut = Description.Create
DesPrdChcKOut("html tag").value = "TABLE"
DesPrdChcKOut("column names").value = "Product Name;Model;Quantity;Price;Total"

For IteratorRow = 1 To 2

'BrwsrCheckOut.WebTable(DesPrdChcKOut).RowCount Step 1
        For IteratorCol = 1 To 3

        'BrwsrCheckOut.WebTable(DesPrdChcKOut).ColumnCount(1) Step 1

    val = BrwsrCheckOut.WebTable(DesPrdChcKOut).GetCellData(IteratorRow, IteratorCol)

    Next

Next
'xl.Activeworkbook.saveAs "D:\Demo\TestData\Shopping_Cart.xls"
xl.Activeworkbook.save
nsheet.SaveAs("D:\Demo\TestData\Shopping_Cart.xls")
xl.ActiveWorkbook.Close



Set xl = nothing
Set nsheet = nothing 

【问题讨论】:

  • 您是否考虑过将 excel 文件作为数据表导入,使用可用的 Datatable 函数在 UFT 中对其进行更新,然后将其导出?可能比创建 excel 对象更简单...

标签: excel qtp hp-uft


【解决方案1】:

我认为这是因为您使用的是 ActiveWorkbook 方法。当您使用活动工作簿方法时,必须非常小心,不要打开其他工作簿/活动状态。

为了避免所有这些,以下可能是更好的方法。

例子

 path = "D:\Demo\TestData\Shopping_Cart.xls"
 Set oxl = CreateObject("Excel.Application")
 Set wbk = oxl.workbooks.open(path)
 sheetname = "Output" ' You can also refer the sheet by index
 Set sheet = wbk.sheets(sheetname)

 'Logic to retreive the data from webtable
 for i=1 to number_of_rows
   ' update the data in excel
 next

 'Save the workbook
 wbk.save
 wbk.close
 oxl.quit

让我知道它是否有效。

【讨论】:

  • 感谢您的浏览,但这并未在 Excel 工作表上保存 webtable 详细信息
  • 我似乎发现了这里的错误。我想在我的代码中使用sheet.cells(row,col)= data。谢谢
  • @nandeshkalyankar,如果您设法自己解决这个问题,您可以将自己的答案添加到解决方案中,甚至接受它
猜你喜欢
  • 2013-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-30
相关资源
最近更新 更多