C# 将网站中的数据提取到包含超链接的Excel文件中
当我使用宏将数据从网页提取到Excel文件时,它会按预期方式将数据保存在Excel文件中。我的要求是,它应将数据与网页中的超链接一起保存在Excel中,如果我们单击Excel中的任何超链接,它应转到特定网站,获取数据并将信息保存在Excel文件或表格中。 我希望这是有道理的。任何帮助都将不胜感激 我的宏代码如下:C# 将网站中的数据提取到包含超链接的Excel文件中,c#,vb.net,vba,excel,C#,Vb.net,Vba,Excel,当我使用宏将数据从网页提取到Excel文件时,它会按预期方式将数据保存在Excel文件中。我的要求是,它应将数据与网页中的超链接一起保存在Excel中,如果我们单击Excel中的任何超链接,它应转到特定网站,获取数据并将信息保存在Excel文件或表格中。 我希望这是有道理的。任何帮助都将不胜感激 我的宏代码如下: Sub GetTable() Dim ieApp As InternetExplorer Dim ieDoc As Object Dim ieTable As Object Dim
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 "website link"
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.forms(0)
.user.Value = "UserNmae
.Password.Value = "password"
.submit
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 "final webpage link"
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.all.Item("AutoNumber1")
'copy the tables html to the clipboard and paste to teh 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.导航“网站链接”
在ieApp.Busy:DoEvents:Loop时执行
直到ieApp.ReadyState=ReadyState\u完成:DoEvents:Loop
设置ieDoc=ieApp.Document
'填写登录表单–从浏览器查看源代码以获取控件名称
使用ieDoc.forms(0)
.user.Value=“UserNmae
.Password.Value=“密码”
提交
以
在ieApp.Busy:DoEvents:Loop时执行
直到ieApp.ReadyState=ReadyState\u完成:DoEvents:Loop
现在我们进入了,进入我们想要的页面
ieApp.导航“最终网页链接”
在ieApp.Busy:DoEvents:Loop时执行
直到ieApp.ReadyState=ReadyState\u完成:DoEvents:Loop
'根据表的id获取表
设置ieDoc=ieApp.Document
设置ieTable=ieDoc.all.Item(“自动编号1”)
'将表格html复制到剪贴板并粘贴到工作表
如果不可否认,那就什么都不是了
Set clip=新数据对象
clip.SetText“&ieTable.outerHTML&”
夹板
表1.选择
表1.范围(“A1”)。选择
Sheet1.1特殊的“Unicode文本”
如果结束
“关闭”er
退出
设置ieApp=Nothing
端接头
您可以使用此代码从web浏览器获取所有超链接,我认为您可以轻松地将数据从列表框导出到excel
Dim links As HtmlElementCollection = WebBrowser1.Document.Links
For Each link As HtmlElement In links
ListBox1.Items.Add(link.GetAttribute("href"))
Next
谢谢您的编辑。您的目标有两个直接的问题。Excel将只显示单元格的前1000个左右字符。Excel超链接仅在单元格级别上运行。好的,假设您在单元格中有超链接,请在单击时测试单元格内容以查找url/超链接,如果是,请导航,然后执行类似的方法上面的od。