把数据库里面的stu表中的数据,导出到excel中
2018-05-13 21:30
856 查看
# 2、写代码实现,把我的数据库里面的stu表中的数据,导出到excel中
#编号 名字 性别
# 需求分析:
# 1、连接好数据库,写好SQL,查到数据 [[1,'name1','男'],[1,'name1','男']]
# 2、写excel,xlwt
import pymysql,xlwt
def my_db(sql):
import pymysql
coon = pymysql.connect(
host='118.xx.xx.xx', user='xxx', passwd='123456',
port=3306, db='xxx', charset='utf8')
cur = coon.cursor() #建立游标
cur.execute(sql)#执行sql
if sql.strip()[:6].upper()=='SELECT':
res = cur.fetchall()
else:
coon.commit()
res = 'ok'
cur.close()
coon.close()
return res
def export(excel_name):
sql = 'select * from stu;'
data = my_db(sql)
book = xlwt.Workbook()
sheet = book.add_sheet('sheet1')
title = ['编号','姓名','性别']
col = 0 #列
for t in title:
sheet.write(0,col,t) #写表头
col+=1
#[[1, 'xxxx', '男'], [1, 'xxxx', '男']]
row = 1 #行数
for d in data: #控制行数
# [1, '牛寒阳', '男']
col = 0
for line in d:#控制列的
sheet.write(row,col,line)
col+=1
row+=1
book.save(excel_name)
# export('stu.xls')
相关文章推荐
- 将数据库里的数据导出到EXCEL中的方法
- 将数据库中的数据按要求导出到excel表中
- java poi实现数据库数据导出到excel
- EXCEL大数据数据库导出
- 把Excel里面的数据导入\导出到SqlServer中
- oracle 数据库中数据导出到excel
- php将数据库数据导出成Excel
- java 数据库 对象 导出Excel数据
- C# DataGgridView 控件绑定数据库sql server,查询数据,导出到Excel
- java读取数据库数据并导出到EXCEL中
- 从数据库导出数据到word、excel、.txt
- asp.net从数据库导出数据到word、excel、txt文本文件
- Excel导入oracle数据,oracle数据库导出excel
- java、Extjs导出数据库里的数据至Excel并下载至本地进行数据备份方法之一——使用servlet
- 从数据库中导出数据以excel格式保存到本地
- 导出数据库数据到Excel
- 从数据库导出数据到word、excel、.txt
- 将数据库中的数据用java程序导出成Excel
- 一个将数据导出到EXCEL的存储过程-数据库专栏,SQL Server
- 利用SQL*Loader将 Excel 数据导出到 Oracle 数据库中(ZZ)