Html 使用excel中的VBA从具有相同ID的两个表的网页导入数据
首先,我要感谢Siddharth Rout&Imdranged先生解决了与使用VBA从安全网站导入excel数据相关的问题,正如我之前讨论的问题,即。和。代码现在运行得很好,但还有一个问题,即我试图从其中导入数据的网页,有两个ID相同的表“report table”。使用VBA代码只能从第一个表复制数据。要同时从两个选项卡(相同ID)表复制数据,VBA中需要进行哪些修改。VBA代码和目标网页源代码再次发布在下面Html 使用excel中的VBA从具有相同ID的两个表的网页导入数据,html,vba,excel,import,Html,Vba,Excel,Import,首先,我要感谢Siddharth Rout&Imdranged先生解决了与使用VBA从安全网站导入excel数据相关的问题,正如我之前讨论的问题,即。和。代码现在运行得很好,但还有一个问题,即我试图从其中导入数据的网页,有两个ID相同的表“report table”。使用VBA代码只能从第一个表复制数据。要同时从两个选项卡(相同ID)表复制数据,VBA中需要进行哪些修改。VBA代码和目标网页源代码再次发布在下面 Sub GetTable() Dim ieApp As Internet
Sub GetTable()
Dim ieApp As InternetExplorer
Dim ieDoc As Object
Dim ieTable As Object
Dim clip As DataObject
'create a new instance of ie
Set ieApp = New InternetExplorer
'you don’t need this, but it’s good for debugging
ieApp.Visible = True
'assume we’re not logged in and just go directly to the login page
ieApp.Navigate "http://cms.indianrail.gov.in/CMSREPORT/JSP/rpt/LoginAction.do?hmode=loginPage"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
Set ieDoc = ieApp.Document
'fill in the login form – View Source from your browser to get the control names
With ieDoc
.getElementById("userId").setAttribute "value", "rlbdgs"
.getElementById("userPassword").setAttribute "value", "123"
'~~> This will select the 2nd radio button as it is `0` based
.getElementsByName("userType")(1).Checked = True
.getElementById("hmode").Click
End With
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
'now that we’re in, go to the page we want
ieApp.Navigate "http://cms.indianrail.gov.in/CMSREPORT/JSP/rpt/GeneralReportAction.do?hmode=drillDown25And26And30GeneralReport&kioskOrManual=K&val=26&wherePart=ZONE_CODE_C=-IR-&lobby=BSL&type=B&startDate=&endDate=&traction=ELEC"
Do While ieApp.Busy: DoEvents: Loop
Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
'get the table based on the table’s id
Set ieDoc = ieApp.Document
Set ieTable = ieDoc.getElementById("report-table")
'copy the tables html to the clipboard and paste to the sheet
If Not ieTable Is Nothing Then
Set clip = New DataObject
clip.SetText "<html>" & ieTable.outerHTML & "</html>"
clip.PutInClipboard
Sheet1.Select
Sheet1.Range("A1").Select
Sheet1.PasteSpecial "Unicode Text"
End If
'close 'er up
ieApp.Quit
Set ieApp = Nothing
End Sub
Sub-GetTable()
Dim ieApp作为InternetExplorer
Dim ieDoc作为对象
可作为对象的
将剪辑变暗为数据对象
'创建ie的新实例
设置ieApp=新的InternetExplorer
您不需要这个,但它有助于调试
ieApp.Visible=True
'假设我们没有登录,直接进入登录页面
ieApp.Navigate“http://cms.indianrail.gov.in/CMSREPORT/JSP/rpt/LoginAction.do?hmode=loginPage"
在ieApp.Busy:DoEvents:Loop时执行
直到ieApp.ReadyState=ReadyState\u完成:DoEvents:Loop
设置ieDoc=ieApp.Document
'填写登录表单–从浏览器查看源代码以获取控件名称
与ieDoc合作
.getElementById(“用户ID”).setAttribute“值”、“rlbdgs”
.getElementById(“用户密码”).setAttribute“值”,“123”
“~~>这将选择第二个单选按钮,因为它基于“0”
.getElementsByName(“用户类型”)(1).Checked=True
.getElementById(“hmode”)。单击
以
在ieApp.Busy:DoEvents:Loop时执行
直到ieApp.ReadyState=ReadyState\u完成:DoEvents:Loop
现在我们进入了,进入我们想要的页面
ieApp.Navigate“http://cms.indianrail.gov.in/CMSREPORT/JSP/rpt/GeneralReportAction.do?hmode=drillDown25And26And30GeneralReport&kioskOrManual=K&val=26&wherePart=ZONE_CODE_C=-IR-&LOB=BSL&type=B&startDate=&endDate=&traction=ELEC“
在ieApp.Busy:DoEvents:Loop时执行
直到ieApp.ReadyState=ReadyState\u完成:DoEvents:Loop
'根据表的id获取表
设置ieDoc=ieApp.Document
设置ieTable=ieDoc.getElementById(“报告表”)
'将表格html复制到剪贴板并粘贴到工作表
如果不可否认,那就什么都不是了
Set clip=新数据对象
clip.SetText“&ieTable.outerHTML&”
夹板
表1.选择
表1.范围(“A1”)。选择
Sheet1.1特殊的“Unicode文本”
如果结束
“关闭”er
退出
设置ieApp=Nothing
端接头
网页来源
<html>
<head>
<title>CREW BOOKED ON TA</title>
<link href="../styles/reportStyle.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
function DoNav(theUrl)
{
//alert(theUrl);
document.location.href = theUrl;
}
</script>
</head>
<body>
<table id="report-table">
<!-- Table header -->
<thead>
<tr>
<th scope="col" class="date" style="border:0px;" colspan="10">Print Date Time: <span>14-08-2014 13:30</span></th>
</tr>
<tr>
<th scope="col" class="report-cris" style="text-align:center;">CRIS</th><th scope="col" class="report-heading" style="text-align:center;" colspan="8">VIGILENCE CONTROL DEVICE (VCD) IN LOCO NOT WORKING(SIGN OFF THROUGH KIOSK)(LOCO SHED WISE)(LAST 24 HOURS)<th scope="col" class="report-cris" style="text-align:center;">CMS</th>
</tr>
<tr style="border:none;">
<th colspan="9" style="border-right:none;">
<span class="report-button" onclick="javascript:history.back();">BACK</span>
<span class="report-button" onclick="javascript:window.print();">PRINT</span>
</th>
<th style="border-left:none;text-align:right;"></th>
</tr>
</table>
<table id="report-table">
<thead>
<tr style="border:none;" align="center">
<th>S.No.</th>
<th>ID</th>
<th>NAME</th>
<th>SIGNOFF DATE</th>
<th>FROM</th>
<th>TO</th>
<th>LOCO NO.</th>
<th>BASE SHED</th>
<th>RAILWAY</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>BINA1482</td>
<td >RAKESH KUMAR BAJPAI</td>
<td>14-08-2014 11:07</td>
<td >BINA</td>
<td>ET </td>
<td>23551 </td>
<td>BRC</td>
<td>WR </td>
</tr>
</tbody>
</table>
* If duration for this report is last 24 hours or from and to date is same, then only last VCD reporting of the loco will be shown.
</body>
</html>
在TA上预订的机组人员
函数DoNav(theUrl)
{
//警报(theUrl);
document.location.href=URL;
}
打印日期时间:2014年8月14日13:30
机车中的应急控制装置(VCD)不工作(通过信息亭签字)(机车棚)(过去24小时)CMS
返回
印刷品
没有。
身份证件
名称
签署日期
从…起
到
机车号。
基棚
铁路
1.
比纳1482
拉凯什·库马尔·巴杰佩
14-08-2014 11:07
比娜
ET
23551
BRC
西铁
*如果此报告的持续时间为24小时,或从开始到结束日期相同,则仅显示loco的最后VCD报告。
请建议解决方案。您上面的行
Set ieTable=ieDoc.all.Item(“报告表”)
应返回所有“报告表”元素的集合。尝试并反复浏览它们。大概是这样的:
For Each ieTable In ieDoc.all.Item("report-table")
'do stuff
Next ieTable
此外,从IE 11开始,
。将不支持所有
。MS建议使用getElementById
在我看来,在同一页面上有两个id相同的元素并不合适,但这可以解释为什么ieDoc.all.Item(“报告表”)返回集合。我想下面的工作应该是:返回到Set ieTable=ieDoc.all.Item(“报告表”),然后循环返回的集合项
但由于服务器当前正在向下查看,因此无法对此进行测试
Set ieTable = ieDoc.all.Item("report-table")
'copy the tables html to the clipboard and paste to the sheet
If Not ieTable Is Nothing Then
oHTML = ""
For i = 0 To ieTable.Length - 1
oHTML = oHTML & ieTable.Item(i).outerHTML
Next i
Set clip = New DataObject
clip.SetText "<html>" & oHTML & "</html>"
clip.PutInClipboard
Sheet1.Select
Sheet1.Range("A1").Select
Sheet1.PasteSpecial "Unicode Text"
End If
Set ieTable=ieDoc.all.Item(“报告表”)
'将表格html复制到剪贴板并粘贴到工作表
如果不可否认,那就什么都不是了
oHTML=“”
对于i=0到ieTable,长度为-1
oHTML=oHTML和ieTable.项目(i).外部TML
接下来我
Set clip=新数据对象
clip.SetText“&oHTML&”
夹板
表1.选择
表1.范围(“A1”)。选择
Sheet1.1特殊的“Unicode文本”
如果结束
对不起,我错贴了未更正的代码。根据Siddharth先生的建议,ieDoc.all.Item已使用getElementById重新计算。现在在上述代码中也可以对其进行编辑。谢谢