从网上找了“VB 采集网页表格数据导入ACCESS 50
代码如下:OptionExplicit'需要添加一个webbrowser控件和一个按钮'引用两个库,在菜单的工程-引用里,找到MicrosoftHTMLobjectlib...
代码如下:
Option Explicit '需要添加一个webbrowser控件 和一个按钮
'引用两个库,在菜单的工程-引用里,找到Microsoft HTML object library和 Microsoft activex data object 2.5/2.6两个库,然后粘贴如下代码
'不明白的百度HI我
Private Sub Command1_Click()
WebBrowser1.Navigate "http://www.szse.cn/main/disclosure/news/xxlb/index.shtml?txtStockCodeORname="
End Sub
Private Sub Form_Load()
WebBrowser1.Stop
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim doc As MSHTML.HTMLDocument
Set doc = WebBrowser1.Document
Dim table As MSHTML.HTMLTable
Dim lrow As MSHTML.HTMLTableRow
Dim cel As MSHTML.HTMLTableCell
Set table = doc.getElementById("REPORTID_tab1")
Dim Conn As ADODB.Connection
Dim FLD() As String
Set Conn = CreateObject("ADODB.Connection")
Conn.open "provider=microsoft.jet.oledb.4.0;data source=d:\DB1.MDB"
Set lrow = table.rows(0)
ReDim FLD(0 To lrow.cells.length) As String
Dim i As Integer
Dim tbname As String
Dim sql As String
tbname = InputBox("输入导入数据库的新建的表的名字", "table1")
For i = 0 To lrow.cells.length - 1
FLD(i) = Replace(Replace(Replace(lrow.cells(i).innerText, "/", ""), "(", ""), ")", "")
sql = sql & FLD(i) & " varchar(255),"
Next i
sql = Left(sql, Len(sql) - 1)
Conn.Execute "create table " & tbname & " (" & sql & ")"
Dim j As Integer
sql = ""
For i = 0 To lrow.cells.length - 1
sql = sql & FLD(i) & ","
Next i
sql = Left(sql, Len(sql) - 1)
Dim vlist As String
For i = 1 To table.rows.length - 1
vlist = ""
Set lrow = table.rows(i)
For j = 0 To lrow.cells.length - 1
vlist = vlist & "'" & lrow.cells(j).innerText & "',"
Next j
vlist = Left(vlist, Len(vlist) - 1)
Conn.Execute "insert into " & tbname & " (" & sql & ") values (" & vlist & ")"
Next i
Conn.Close
Set Conn = Nothing
End Sub 展开
Option Explicit '需要添加一个webbrowser控件 和一个按钮
'引用两个库,在菜单的工程-引用里,找到Microsoft HTML object library和 Microsoft activex data object 2.5/2.6两个库,然后粘贴如下代码
'不明白的百度HI我
Private Sub Command1_Click()
WebBrowser1.Navigate "http://www.szse.cn/main/disclosure/news/xxlb/index.shtml?txtStockCodeORname="
End Sub
Private Sub Form_Load()
WebBrowser1.Stop
End Sub
Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
Dim doc As MSHTML.HTMLDocument
Set doc = WebBrowser1.Document
Dim table As MSHTML.HTMLTable
Dim lrow As MSHTML.HTMLTableRow
Dim cel As MSHTML.HTMLTableCell
Set table = doc.getElementById("REPORTID_tab1")
Dim Conn As ADODB.Connection
Dim FLD() As String
Set Conn = CreateObject("ADODB.Connection")
Conn.open "provider=microsoft.jet.oledb.4.0;data source=d:\DB1.MDB"
Set lrow = table.rows(0)
ReDim FLD(0 To lrow.cells.length) As String
Dim i As Integer
Dim tbname As String
Dim sql As String
tbname = InputBox("输入导入数据库的新建的表的名字", "table1")
For i = 0 To lrow.cells.length - 1
FLD(i) = Replace(Replace(Replace(lrow.cells(i).innerText, "/", ""), "(", ""), ")", "")
sql = sql & FLD(i) & " varchar(255),"
Next i
sql = Left(sql, Len(sql) - 1)
Conn.Execute "create table " & tbname & " (" & sql & ")"
Dim j As Integer
sql = ""
For i = 0 To lrow.cells.length - 1
sql = sql & FLD(i) & ","
Next i
sql = Left(sql, Len(sql) - 1)
Dim vlist As String
For i = 1 To table.rows.length - 1
vlist = ""
Set lrow = table.rows(i)
For j = 0 To lrow.cells.length - 1
vlist = vlist & "'" & lrow.cells(j).innerText & "',"
Next j
vlist = Left(vlist, Len(vlist) - 1)
Conn.Execute "insert into " & tbname & " (" & sql & ") values (" & vlist & ")"
Next i
Conn.Close
Set Conn = Nothing
End Sub 展开
2个回答
2015-08-18
展开全部
经试验,你提供的代码完全正确,请确定以下是否完成:
1、在D:\文件夹下,新建一个DB.mdb的access文件
2、添加一个webbrowser控件 和一个按钮,名称为:webbrowser1和command1
3、引用两个库,在菜单的工程-引用里,找到Microsoft HTML object library和 Microsoft activex data objects2.8 labrary
4、运行后,点command1,输入表名
5、运行完成后,打开db1.mdb查看
1、在D:\文件夹下,新建一个DB.mdb的access文件
2、添加一个webbrowser控件 和一个按钮,名称为:webbrowser1和command1
3、引用两个库,在菜单的工程-引用里,找到Microsoft HTML object library和 Microsoft activex data objects2.8 labrary
4、运行后,点command1,输入表名
5、运行完成后,打开db1.mdb查看
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询