python 数据库导出excle 分sheet
来源:互联网 发布:淘宝网天天特价连衣裙 编辑:程序博客网 时间:2024/05/22 18:55
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import xlwt
import MySQLdb
def export(host,user,password,dbname,table_name,outputpath):
conn = MySQLdb.connect(host=host,port=3307,user=user,passwd=password,db=dbname,charset='utf8')
cursor = conn.cursor()
count = cursor.execute("select distinct SNAME from "+table_name+"")
cursor.scroll(0,mode='absolute')
resultsSNAME =cursor.fetchall()
workbook = xlwt.Workbook()
for sname in resultsSNAME:
sql="select * from "+table_name+" where SNAME='%s' order by TIME desc" % (sname[0])
print sql
cursor.execute(sql)
results = cursor.fetchall()
fields = cursor.description
for s in range(0,len(results)/65535+1):
print s
sheet = workbook.add_sheet(sname[0] ,cell_overwrite_ok=True)
for field in range(0,len(fields)):
sheet.write(0,field,fields[field][0])
row = 1
col = 0
if s<len(results)/65535:
for row in range(1,65535):
for col in range(0,len(fields)):
sheet.write(row,col,u'%s'%results[65534*s+row-1][col])
else :
for row in range(1,len(results)%65534+1):
for col in range(0,len(fields)):
sheet.write(row,col,u'%s'%results[65534*s+row-1][col])
workbook.save(outputpath)
if __name__ == "__main__":
export('host','user','passwordl','dbname','file.xlsx')
print "over"
# -*- coding: UTF-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import xlwt
import MySQLdb
def export(host,user,password,dbname,table_name,outputpath):
conn = MySQLdb.connect(host=host,port=3307,user=user,passwd=password,db=dbname,charset='utf8')
cursor = conn.cursor()
count = cursor.execute("select distinct SNAME from "+table_name+"")
cursor.scroll(0,mode='absolute')
resultsSNAME =cursor.fetchall()
workbook = xlwt.Workbook()
for sname in resultsSNAME:
sql="select * from "+table_name+" where SNAME='%s' order by TIME desc" % (sname[0])
print sql
cursor.execute(sql)
results = cursor.fetchall()
fields = cursor.description
for s in range(0,len(results)/65535+1):
print s
sheet = workbook.add_sheet(sname[0] ,cell_overwrite_ok=True)
for field in range(0,len(fields)):
sheet.write(0,field,fields[field][0])
row = 1
col = 0
if s<len(results)/65535:
for row in range(1,65535):
for col in range(0,len(fields)):
sheet.write(row,col,u'%s'%results[65534*s+row-1][col])
else :
for row in range(1,len(results)%65534+1):
for col in range(0,len(fields)):
sheet.write(row,col,u'%s'%results[65534*s+row-1][col])
workbook.save(outputpath)
if __name__ == "__main__":
export('host','user','passwordl','dbname','file.xlsx')
print "over"
阅读全文
0 0
- python 数据库导出excle 分sheet
- 分sheet导出excel 大数据量导出
- 导出excel,并支持分sheet导出
- C#导出分Sheet的Excel文件
- C#导出分Sheet的Excel文件
- C#导出分Sheet的Excel文件
- ASP.NET 分Sheet导出EXCEL 2003
- 关于数据库导出excle上限问题
- Excle导出
- 导出EXCLE
- excle导出
- 导出excle
- excle导出
- excle导出
- JXL实现excel导出案例(分sheet页)
- JXL实现excel导出案例(分sheet页)
- npoi导出超过65000行excel分sheet页。
- 数据导出Excle
- Unity 物体根据鼠标移动而转动
- 一枚资深设计师的十年成长漂泊故事
- map,hash_map和unordered_map 实现比较
- NuttX 启动流程
- 2017/12/19 第十天培训
- python 数据库导出excle 分sheet
- 给设计师的会议沟通完全指南
- Linux多线程之互斥锁
- 二、Spring Cloud简介
- maven 使用问题记录
- 关于二叉树的非递归遍历
- 向CMD窗口发送CTRL+C 只能第一次生效
- SCAU 8615 快乐
- 深度学习: marginal cost (边际成本)