最近有个测试,需要将本地的Excel数据导入到SQL Server中,所以就写了一个这个脚本,供有需要的同学进行参考。因为在此演示测试,所以准备的数据都比较简单。
我们准备将本地的Excel的A列插入到数据库中的Username列
首先准备本地的Excel数据
然后准备数据库及表结构
开始上脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
Dim DB,objRecordSet Dim Excel 'SQL Server服务器地址 Dim SQLserver 'SQL Server 数据库 Dim Database 'SQL Server登录名 Dim uid 'SQL Server登陆密码 Dim pwd
'SQL Server 数据库表名 Dim Tablename 'SQL Server数据库表列字段 Dim Column 'Excel表路劲 Dim XLSPath SQLserver = "192.168.7.170"
Database = "PasswordInfo"
uid = "sa"
pwd = "Password"
tablename = "UserServerInfo"
column = "Username"
XLSPath = "d:\users.xlsx"
Set DB = CreateObject( "ADODB.Connection" )
Set objRecordSet = CreateObject( "ADODB.RECORDSET" )
Set Excel = CreateObject( "Excel.Application" )
DB.Open "Driver=SQL Server;server=" &SQLserver& ";Database=" &Database& ";uid=" &uid& ";pwd=" & pwd & ";"
Excel.DisplayAlerts=FALSE Excel.visible=FALSE Excel.workbooks. open (XLSPath)
line = 1 value = Excel.ActiveSheet.Cells(line,1).Value Do While value <> ""
line = line + 1 insertDB(value) value = Excel.ActiveSheet.Cells(line,1).Value Loop msgbox "Finish"
Function insertDB(value) sql = "insert into " &Tablename & "(" & Column & ") VALUES (" & "'" &value& "'" & ")"
msgbox sql DB.Execute(sql) End Function |
我们开始开执行,开始插入第一条数据
执行完成
我们查看数据库表
因为vbs脚本里面有连接数据库的账户及密码,比较敏感,所以我们为了安全,我们需要将vbs转化成exe,但是这样的话,文件路劲是写死的,不方便,所以我们需要将源文件的路劲更改成浏览框。
1
2
3
4
5
|
更换前: 'XLSPath = "d:\users.xlsx"
更换后: msgbox "请选择源文件"
XLSPath = CreateObject( "WScript.Shell" ).Exec( "mshta vbscript:" "<input type=file id=f><script>f.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(f.value)[close()];</script>" "" ).StdOut.ReadAll
|
修改后的整体代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
Dim DB,objRecordSet Dim Excel 'SQL Server服务器地址 Dim SQLserver 'SQL Server 数据库 Dim Database 'SQL Server登录名 Dim uid 'SQL Server登陆密码 Dim pwd
'SQL Server 数据库表名 Dim Tablename 'SQL Server数据库表列字段 Dim Column 'Excel表路劲 Dim XLSPath SQLserver = "192.168.7.170"
Database = "PasswordInfo"
uid = "sa" pwd = "Password"
tablename = "UserServerInfo" column = "Username"
'XLSPath = "d:\users.xlsx"
msgbox "请选择源文件"
XLSPath = CreateObject( "WScript.Shell" ).Exec( "mshta vbscript:" "<input type=file id=f><script>f.click();new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).Write(f.value)[close()];</script>" "" ).StdOut.ReadAll
Set DB = CreateObject( "ADODB.Connection" )
Set objRecordSet = CreateObject( "ADODB.RECORDSET" )
Set Excel = CreateObject( "Excel.Application" )
DB.Open "Driver=SQL Server;server=" &SQLserver& ";Database=" &Database& ";uid=" &uid& ";pwd=" & pwd & ";" Excel.DisplayAlerts=FALSE Excel.visible=FALSE Excel.workbooks. open (XLSPath)
line = 1 value = Excel.ActiveSheet.Cells(line,1).Value Do While value <> "" line = line + 1
insertDB(value)
value = Excel.ActiveSheet.Cells(line,1).Value
Loop
msgbox "Finish"
Function insertDB(value) sql = "insert into " &Tablename & "(" & Column & ") VALUES (" & "'" &value& "'" & ")" 'msgbox sql
DB.Execute(sql)
End Function
|
执行结果:
我们将提示插入的数据的msgbox注释,然后执行尝试
执行后,会提示完成
我们再次查看数据库结果
本文转自 高文龙 51CTO博客,原文链接:http://blog.51cto.com/gaowenlong/1925458,如需转载请自行联系原作者