# -*- coding: utf-8 -*-
import win32com.client as win32
import MySQLdb
from pyExcelerator import *
addr = raw_input('输入地址: ')
def readdb():
# 封装数据
#连接数据库
conn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='sign', charset="utf8")
#获取游标
cursor = conn.cursor()
cursor.execute('select * from message_user')
#取得所有数据
result = cursor.fetchall()
#取得数据字段
fields = cursor.description
#关闭游标和查询链接
cursor.close()
conn.close()
return result,fields,len(result)
def checkout(result,fields,address,length):
#创建新excel文件
create(address)
app = 'Excel'
#指定是应用程序是Excel.Application并得到excel.Application对象
xl = win32.Dispatch('%s.application' % app)
#得到Workbooks
xlbook = xl.Workbooks.Open(address)
#得到名称为data的Sheet对象
sh1 = xlbook.Worksheets('data')
for l in range(1,len(fields)+1):
sh1.Cells(1,l).Value=fields[l-1][0]
#将读取的数据填入到相应的行列中
for h in range(2,len(result)+2):
for l in range(1,len(fields)+1):
sh1.Cells(h,l).Value=result[h-2][l-1]
#判断是否导入成功
if int(sh1.Cells(h,1)) == len(result):
#关闭EXCEL程序
xl.Application.Quit()
print '导出成功'
return True
else:
xl.Application.Quit()
print '导出失败'
return False
def create(address):
#新建文件
w = Workbook()
ws = w.add_sheet(r'data')
w.save(address)
return True
if __name__ == "__main__":
#传参
a,b,d = readdb()
c = addr + r'userdb.xls'
checkout(a,b,c,d)
上面的方法比较繁琐。尤其是往excel里面写数据的时候完全也可以用pyExcelerator搞定。而且解决了上面代码保存以后不能自己关闭excel的问题(需要点击确认保存修改——>OK)
# -*- coding: utf-8 -*-
import MySQLdb
from pyExcelerator import *
addr = raw_input('address: ')
def readdb():
# 封装数据
#连接数据库
conn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='sign', charset="utf8")
#获取游标
cursor = conn.cursor()
cursor.execute('select * from message_user')
#取得所有数据
result = cursor.fetchall()
#取得数据字段
fields = cursor.description
#关闭游标和查询链接
cursor.close()
conn.close()
return result,fields,len(result)
def checkout(result,fields,address,length):
#新建文件
w = Workbook()
ws = w.add_sheet(r'data')
for l in range(len(fields)):
ws.write(0,l,fields[l][0])
#将读取的数据填入到相应的行列中
for h in range(1,len(result)+1):
for l in range(len(fields)):
ws.write(h,l,result[h-1][l])
#判断是否导入成功
if h == len(result):
#关闭EXCEL程序
w.save(address)
print 'Sucess'
return True
else:
w.save(address)
print 'Fail'
return False
if __name__ == "__main__":
#传参
a,b,d = readdb()
c = addr + r'userdb.xls'
checkout(a,b,c,d)
要在网页上下载导出的话可以用下面的方法:
def pull(request):
result,fields,length = readdb()
#新建文件
w = Workbook()
ws = w.add_sheet(r'data')
for l in range(len(fields)):
ws.write(0,l,fields[l][0])
#将读取的数据填入到相应的行列中
for h in range(1,length+1):
for l in range(len(fields)):
ws.write(h,l,result[h-1][l])
response = HttpResponse(mimetype='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename=userdb.xls'
w.save(response)
return response
def readdb():
# 封装数据
#连接数据库
conn = MySQLdb.connect(host='192.168.2.210', user='root', passwd='root', db='hbl_3', charset="utf8")
#获取游标
cursor = conn.cursor()
cursor.execute('select * from XXXX')
#取得所有数据
result = cursor.fetchall()
#取得数据字段
fields = cursor.description
#关闭游标和查询链接
cursor.close()
conn.close()
return result,fields,len(result)