码迷,mamicode.com
首页 > 其他好文 > 详细

通用导出excel

时间:2019-02-22 11:54:33      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:color   nec   user   cursor   root   host   des   自动   exec   

自动查询下标

fileds=[id,name,sex]
for index,filed in enumerate(fileds):#enumerate,可以查询列表每个字段的下标
    print(index,filed)
# 结果:
# 0 id
# 1 name
# 2 sex

 

通用导出excel-只导出字段

import pymysql,xlwt
#
# def export_excel(table_name):
#     host,user,passwd,db=‘192.168.0.12‘,‘root‘,‘myjcyf‘,‘us_sys‘
#     coon=pymysql.connect(host=host,user=user,passwd=passwd,db=db,port=3306,charset=‘utf8‘)
#     cur=coon.cursor() #建立游标
#     sql=‘select * from %s;‘%table_name
#     cur.execute(sql)#执行sql
#     fileds=[filed[0] for filed in cur.description]#所有字段
#     all_date=cur.fetchall()
#     book=xlwt.Workbook()
#     sheet=book.add_sheet(‘sheet1‘)
#     col=0
#     for filed in fileds:
#         sheet.write(0,col,filed)
#         col+=1
#     book.save(‘%s.xls‘%table_name)
# export_excel(‘us_sys.stu‘)#导出excel

通用导出excel

标签:color   nec   user   cursor   root   host   des   自动   exec   

原文地址:https://www.cnblogs.com/ruijie/p/10417414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!