用Python将Excel数据导入到SQL Server的例子
作者:azaho 时间:2021-08-21 19:24:30
使用环境:Win10 x64 Python:3.6.4 SqlServer:2008R2
因为近期需要将excel导入到SQL Server,但是使用的是其他语言,闲来无事就尝试着用python进行导入,速度还是挺快的,1w多条数据,也只用了1s多,代码也比较简单,就不多解释了。
用到的库有xlrd(用来处理excel),pymssql(用来连接使用sql server)
import xlrd
import pymssql
import datetime
# 连接本地sql server 地址 用户名 密码 数据库
conn = pymssql.connect("127.0.0.1:1433", "sa", "888888", "Test")
# 建立cursor
cursor = conn.cursor()
# excel文件
fname = "XXXX.xls"
#打开文件
bk = xlrd.open_workbook(fname)
#打开工作表
sh = bk.sheets()[0]
#获取行数
start_time=datetime.datetime.now()
sql3=''
# 遍历所有行
for i in range(1,sh.nrows):
a = []
sql = '('
# 遍历所有列
for j in range(sh.ncols):
# 将excel每一列的值用,隔开
sql += "'" + str(sh.cell(i, j).value) + "'" + ','
# 组合成sql语句(value1,value2,value3,,)
sql2 = sql.strip(",")
sql3 += sql2.strip()+'),'
# 1000行执行一次sql
if i%1000==0:
sql3 = sql3.rstrip(",")
sql1 = "insert into Flow(ID, SaleDate,danwei,danweiname,huowuid,huowuname,chandi,guge,num,danjia,back,pihao,youxiaodate,jibendanwei,Userid,UserdanweiName,kehuquyu,kehuleixing,type) values %s " % sql3
# 执行sql语句
cursor.execute(sql1)
sql = ""
sql3=""
sql3 = sql3.rstrip(",")
sql1 = "insert into Flow(ID, SaleDate,danwei,danweiname,huowuid,huowuname,chandi,guge,num,danjia,back,pihao,youxiaodate,jibendanwei,Userid,UserdanweiName,kehuquyu,kehuleixing,type) values %s " % sql3
cursor.execute(sql1)
# commit提交变更
conn.commit()
# 结束时间
end_time = datetime.datetime.now()
speed = end_time - start_time
# 打印花费时间
print(speed)
来源:https://blog.csdn.net/zhaogeno1/article/details/80808246
标签:Python,Excel,SQL,Server
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
php5.4传引用时报错问题分析
2023-11-14 15:03:17
Symfony2框架创建项目与模板设置实例详解
2023-11-20 23:36:39
![](https://img.aspxhome.com/file/2023/9/92809_0s.jpg)
python中的global关键字的使用方法
2023-07-15 13:26:50
浅析Python中的赋值和深浅拷贝
2021-09-10 14:30:14
![](https://img.aspxhome.com/file/2023/9/78549_0s.png)
python妹子图简单爬虫实例
2022-12-11 05:47:50
python调用ffmpeg命令行工具便捷操作视频示例实现过程
2023-12-19 07:48:22
Python之Pygame的Draw绘图
2022-11-29 18:51:18
交互设计实用指南系列(9)—一次点击
2010-02-08 12:42:00
![](https://img.aspxhome.com/file/UploadPic/20102/8/guanji-80s.jpg)
win10下python3.5.2和tensorflow安装环境搭建教程
2022-11-05 15:56:21
![](https://img.aspxhome.com/file/2023/2/80622_0s.png)
解决python执行不输出系统命令弹框的问题
2022-10-01 10:25:02
Mootools 1.2教程(3)——数组使用简介
2008-11-17 20:16:00
python获取命令行参数实例方法讲解
2022-12-10 06:40:32
js实现的全国省市二级联动下拉选择菜单完整实例
2023-09-09 05:21:01
![](https://img.aspxhome.com/file/2023/4/56304_0s.jpg)
Python之进行URL编码案例讲解
2023-03-31 03:46:58
![](https://img.aspxhome.com/file/2023/1/87761_0s.png)
解决python 使用openpyxl读写大文件的坑
2021-06-20 17:03:24
![](https://img.aspxhome.com/file/2023/2/95822_0s.jpg)
基于Python编写一个B站全自动抽奖的小程序
2021-05-03 02:11:07
![](https://img.aspxhome.com/file/2023/8/79788_0s.jpg)
用代码帮你了解Python基础(3)
2021-03-20 07:22:46
Pycharm生成可执行文件.exe的实现方法
2023-07-19 12:09:54
![](https://img.aspxhome.com/file/2023/3/64273_0s.jpg)
基于 Python实现云服务器的CDN域名远程鉴权配置
2023-07-31 20:44:51
![](https://img.aspxhome.com/file/2023/1/97761_0s.webp)
开心网让人很不开心
2009-04-05 15:56:00
![](https://img.aspxhome.com/file/UploadPic/20094/5/k2009331104113-27s.gif)