用VBA语言调用sql数据库数据到excel中
答案:1 悬赏:20 手机版
解决时间 2021-01-18 02:58
- 提问者网友:不爱我么
- 2021-01-17 08:13
用VBA语言调用sql数据库数据到excel中
最佳答案
- 五星知识达人网友:春色三分
- 2021-01-17 08:32
VBE 工具 引用,选中 Microsoft ActiveX Data Object 2.8 Library
我不清楚是不是UFDATA_013_2008表的VENDOR列的数据,如果不是,你自己改一下SQL语句吧,sql = "select VENDOR from UFDATA_013_2008"
Sub SQL数据导入()
Dim cn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim str As String
Dim sql As String
Dim i As Integer
str = "Provider=SQLOLEDB;Data Source=SQLSERVER服务器名或IP;DATABASE=数据库;UID=用户名;PWD=密码"
cn.Open str
Set rs = New ADODB.Recordset
sql = "select VENDOR from UFDATA_013_2008"
rs.Open sql, cn, adOpenStatic, adLockOptimistic
If Not rs.EOF Then
For i = 1 To rs.RecordCount
Sheet2.Cells(i, 1).Value = rs.Fields(0).Value
Next i
rs.MoveNext
End If
rs.Close
cn.Close
End Sub
我不清楚是不是UFDATA_013_2008表的VENDOR列的数据,如果不是,你自己改一下SQL语句吧,sql = "select VENDOR from UFDATA_013_2008"
Sub SQL数据导入()
Dim cn As New ADODB.Connection
Dim rs As ADODB.Recordset
Dim str As String
Dim sql As String
Dim i As Integer
str = "Provider=SQLOLEDB;Data Source=SQLSERVER服务器名或IP;DATABASE=数据库;UID=用户名;PWD=密码"
cn.Open str
Set rs = New ADODB.Recordset
sql = "select VENDOR from UFDATA_013_2008"
rs.Open sql, cn, adOpenStatic, adLockOptimistic
If Not rs.EOF Then
For i = 1 To rs.RecordCount
Sheet2.Cells(i, 1).Value = rs.Fields(0).Value
Next i
rs.MoveNext
End If
rs.Close
cn.Close
End Sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯