从网络表中提取信息到Excel

时间:2018-10-22 13:49:19

标签: html asp.net excel vba html-table

此问题的后续跟进(Open IE and Interact with Web Form using Excel VBA)…

在用户QHarr的帮助下,我现在可以与Web表单进行交互,并提交正确的信息以显示所需的表格。

现在我需要的是excel能够读取该网页,查找表并将数据提取到电子表格内的单独选项卡中...而且我不知道从哪里开始进行制作这样吗?

该页面的HTML在下面,我相信它是一个ASP页面,它使用提交的信息来生成正确的信息表...

<!--include file="../../Connections/SQLStore_00103_DT_Performance_Packs.asp" -->
<!--include file="../../Connections/SQL.asp" -->


<!--include file="JSClass/FusionCharts.asp" -->
<script language="JavaScript" src="JSClass/FusionCharts.js"></script>

<!--include file="Includes/SaveCookies.asp" -->




<br>





<body>

</body>
</html>




<link href="../CSS/KPI_Style.css" rel="stylesheet" type="text/css"/>
<?xml version="1.0" encoding="iso-8859-1"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Store Performance Report</title>

<style type="text/css">
<!--
.style49 {font-size: 7pt}
.style50 {font-size: 16pt; color: #FF9933; }
.style60 {font-size: 12pt}
-->
</style>
</head>




<body >


<!--include file="../Store_Performance/Includes/Disable_copying.asp" -->

<style type="text2/css">
<!--
.style45 {font-size: 12px}
-->
</style>

<table cellpadding='0' cellspacing='0' border='0' width='95%'><tr  class='formtextsmall'><td class='border9' rowspan='2' valign='bottom' align='center' class='formtextsmall'><a href='Summary_Page_Sum.asp?ranking=1'class='vert'></a></td><td class='border9' colspan='5' valign='bottom' align='center'   bgcolor='#fff762' >Sales</td><td class='border9' colspan='3' valign='bottom' align='center'    ></td><td class='border9' colspan='3' valign='bottom' align='center'  bgcolor='#fe7845'  ></td><td class='border8' colspan='15' valign='bottom' align='center'  bgcolor='#76df64' ></td></tr><tr class='formtextsmall'><td class='border3' valign='bottom' align='center' nowrap  bgcolor='#fff762' ><a href='Summary_Page_Sum.asp?ranking=2' class='vert'> £&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap><a href='Summary_Page_Sum.asp?ranking=3' class='vert'>% to Tgt&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap><a href='Summary_Page_Sum.asp?ranking=4' class='vert'>No</a></td><td class='border3' valign='bottom' align='center' nowrap><a href='Summary_Page_Sum.asp?ranking=5' class='vert'>1&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap><a href='Summary_Page_Sum.asp?ranking=6' class='vert'>2&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap><a href='Summary_Page_Sum.asp?ranking=50' class='vert'>3&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap><a href='Summary_Page_Sum.asp?ranking=51' class='vert'>4&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap><a href='Summary_Page_Sum.asp?ranking=44' class='vert'>5&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap  bgcolor='#fe7845'  ><a href='Summary_Page_Sum.asp?ranking=7' class='vert'>6&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap><a href='Summary_Page_Sum.asp?ranking=8' class='vert'>7&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap><a href='Summary_Page_Sum.asp?ranking=9' class='vert'>8&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap   bgcolor='#76df64'><a href='Summary_Page_Sum.asp?ranking=55' class='vert'>&nbsp;9&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap   bgcolor='#76df64'><a href='Summary_Page_Sum.asp?ranking=43' class='vert'>Total Retention&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap bgcolor='#CCFFFF'  ><a href='Summary_Page_Sum.asp?ranking=41' class='vert'>Comp Retention&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap bgcolor='#CCFFFF'  ><a href='Summary_Page_Sum.asp?ranking=12' class='vert'>Comp R&S SR %&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap bgcolor='#CCFFFF'  ><a href='Summary_Page_Sum.asp?ranking=14' class='vert'>Tablet R&S SR %&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap bgcolor='#CCFFFF'  ><a href='Summary_Page_Sum.asp?ranking=22' class='vert'>Anti Virus SR %&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap bgcolor='#CCFFFF'  ><a href='Summary_Page_Sum.asp?ranking=16' class='vert'>Cloud SR %&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap bgcolor='#CCFFFF'  ><a href='Summary_Page_Sum.asp?ranking=18' class='vert'>Setup SR%&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap bgcolor='#CCFFFF'  ><a href='Summary_Page_Sum.asp?ranking=20' class='vert'>Office SR%&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap bgcolor='#FFCCFF' ><a href='Summary_Page_Sum.asp?ranking=42' class='vert' >Other Retention&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap bgcolor='#FFCCFF' ><a href='Summary_Page_Sum.asp?ranking=26' class='vert'>MDA R&S  SR%&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap bgcolor='#FFCCFF' ><a href='Summary_Page_Sum.asp?ranking=24' class='vert'>Vision R&S  SR%&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap bgcolor='#FFCCFF' ><a href='Summary_Page_Sum.asp?ranking=28' class='vert'>Other R&S  SR%&nbsp;</a></td><td class='border3' valign='bottom' align='center' nowrap bgcolor='#FFCCFF' ><a href='Summary_Page_Sum.asp?ranking=35' class='vert'>Bracket/Stand/AV SR%&nbsp;</a></td><td class='border14' valign='bottom' align='center' nowrap bgcolor='#FFCCFF' ><a href='Summary_Page_Sum.asp?ranking=30' class='vert'>Premium Cable SR%&nbsp;</a></td></tr>

<tr class='formtextsmall'><td class='border3' align='left'  class='text'>&nbsp;(427) John R</td><td class='border3' align='right' class='text'   bgcolor='#fff762' >-£60&nbsp;</td><td class='border3' align='right' class='text'>&nbsp;0.0&nbsp;</td><td class='border3' align='right' class='text'>&nbsp;1&nbsp;</td><td class='border3' align='right' class='text'>£0&nbsp;</td><td class='border3' align='right' class='text'>£0&nbsp;</td><td class='border3' align='right' class='text'>0&nbsp;</td><td class='border3' align='right' class='text'>0&nbsp;</td><td class='border3' align='right' class='text'>0&nbsp;</td><td class='border3' align='right' class='text' bgcolor='#fe7845'  >&nbsp;0.0&nbsp;</td><td class='border3' align='right' class='text'>-£7&nbsp;</td><td class='border3' align='right' class='text'>-£7&nbsp;</td><td class='border3' align='center' class='text'   bgcolor='#76df64'>&nbsp;0&nbsp;</td><td class='border3' align='right' class='text'   bgcolor='#76df64'>&nbsp;88.9%&nbsp;</td><td class='border3' align='right' class='text'  bgcolor='#CCFFFF'  >&nbsp;100.0%&nbsp;</td><td class='border3' align='right' class='text'>&nbsp;0.0&nbsp;</td><td class='border3' align='right' class='text' bgcolor='#CCFFFF'  >&nbsp;0.0&nbsp;</td><td class='border3' align='right' class='text' bgcolor='#CCFFFF'  >&nbsp;0.0&nbsp;</td><td class='border3' align='right' class='text' bgcolor='#CCFFFF'  >&nbsp;0.0&nbsp;</td><td class='border3' align='right' class='text' bgcolor='#CCFFFF'  >&nbsp;0.0&nbsp;</td><td class='border3' align='right' class='text'>&nbsp;0.0&nbsp;</td><td class='border3' align='right' class='text' bgcolor='#FFCCFF' >&nbsp;85.7%&nbsp;</td><td class='border3' align='right' class='text'>&nbsp;0.0&nbsp;</td><td class='border3' align='right' class='text'>&nbsp;0.0&nbsp;</td><td class='border3' align='right' class='text'>&nbsp;0.0&nbsp;</td><td class='border3' align='right' class='text'>&nbsp;0.0&nbsp;</td><td class='border14' align='right' class='text'>&nbsp;0.0&nbsp;</td></tr><tr class='formtextsmall'><td align='center' class='border11' colSpan='35'>&nbsp;</td></tr></table><br>
<!--include file="Includes/League_Page_Links.asp"-->
<style type="text/css">
<!--
.style1 {
    font-size: 10px;
    color: #FFFFFF;
}
-->
</style>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
  <td valign="bottom" bgcolor="#001446"><div align="right" class="xbig style1 style1">A</div></td> 

  </tr>
</table>






</body>
</html>

如果有人可以帮助我开发代码以从整个表中提取数据(示例中仅包含1行,但最多可以包含150行),我将非常感激。

谢谢。

1 个答案:

答案 0 :(得分:0)

通过循环行和列,看起来像一个标准的写出表。以下假定您正在使用IE,并且已完全加载IE.document。摘录中只显示了一张桌子。

Dim html As New HTMLDocument, hTable As HTMLTable, c As Long, r As Long, tr As Object, tc As Object

Set hTable = IE.document.querySelector("table") '<== get the table

With ThisWorkbook.Worksheets("Sheet1")
    For Each tr In hTable.getElementsByTagName("tr") '<== get the rows and loop over them
        r = r + 1: c = 1 
        For Each tc In tr.getElementsByTagName("td")  '<== get the columns within a row and loop over them
            .Cells(r, c).Value = tc.innerText
            c = c + 1
        Next
    Next
End With