VBS将本地的Excel数据导入到SQL Server中

本文涉及的产品
云数据库 RDS SQL Server,独享型 2核4GB
简介:

最近有个测试,需要将本地的Excel数据导入到SQL Server中,所以就写了一个这个脚本,供有需要的同学进行参考。因为在此演示测试,所以准备的数据都比较简单。

我们准备将本地的Excel的A列插入到数据库中的Username列

首先准备本地的Excel数据

image

然后准备数据库及表结构

image

开始上脚本

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

我们开始开执行,开始插入第一条数据

image

image

image

执行完成

image

我们查看数据库表

image

因为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

执行结果:

clipboard

clipboard[1]

我们将提示插入的数据的msgbox注释,然后执行尝试

image

执行后,会提示完成

image

我们再次查看数据库结果

image



本文转自 高文龙 51CTO博客,原文链接:http://blog.51cto.com/gaowenlong/1925458,如需转载请自行联系原作者

相关实践学习
使用SQL语句管理索引
本次实验主要介绍如何在RDS-SQLServer数据库中,使用SQL语句管理索引。
SQL Server on Linux入门教程
SQL Server数据库一直只提供Windows下的版本。2016年微软宣布推出可运行在Linux系统下的SQL Server数据库,该版本目前还是早期预览版本。本课程主要介绍SQLServer On Linux的基本知识。 相关的阿里云产品:云数据库RDS&nbsp;SQL Server版 RDS SQL Server不仅拥有高可用架构和任意时间点的数据恢复功能,强力支撑各种企业应用,同时也包含了微软的License费用,减少额外支出。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/sqlserver
相关文章
|
20小时前
|
SQL 存储 数据库连接
LabVIEW与SQL Server 2919 Express通讯
LabVIEW与SQL Server 2919 Express通讯
|
1天前
|
SQL Windows
安装SQL Server 2005时出现对性能监视器计数器注册表值执行系统配置检查失败的解决办法...
安装SQL Server 2005时出现对性能监视器计数器注册表值执行系统配置检查失败的解决办法...
|
2天前
|
SQL 数据可视化 Oracle
这篇文章教会你:从 SQL Server 移植到 DM(上)
这篇文章教会你:从 SQL Server 移植到 DM(上)
|
2天前
|
SQL 关系型数据库 数据库
SQL Server语法基础:入门到精通
SQL Server语法基础:入门到精通
SQL Server语法基础:入门到精通
|
2天前
|
SQL 存储 网络协议
SQL Server详细使用教程
SQL Server详细使用教程
23 2
|
2天前
|
SQL 存储 数据库连接
C#SQL Server数据库基本操作(增、删、改、查)
C#SQL Server数据库基本操作(增、删、改、查)
5 0
|
2天前
|
SQL 存储 小程序
数据库数据恢复—Sql Server数据库文件丢失的数据恢复案例
数据库数据恢复环境: 5块硬盘组建一组RAID5阵列,划分LUN供windows系统服务器使用。windows系统服务器内运行了Sql Server数据库,存储空间在操作系统层面划分了三个逻辑分区。 数据库故障: 数据库文件丢失,主要涉及3个数据库,数千张表。数据库文件丢失原因未知,不能确定丢失的数据库文件的存放位置。数据库文件丢失后,服务器仍处于开机状态,所幸未写入大量数据。
数据库数据恢复—Sql Server数据库文件丢失的数据恢复案例
|
3天前
|
SQL 存储 关系型数据库
SQL Server详细使用教程及常见问题解决
SQL Server详细使用教程及常见问题解决
|
4天前
|
SQL 安全 数据库
SQL Server 备份和还原
SQL Server 备份和还原
|
4天前
|
SQL 存储 安全
SQL Server 权限管理
SQL Server 权限管理