其他分享
首页 > 其他分享> > 循环导出所有行和列

循环导出所有行和列

作者:互联网

import pymysql,xlwt
def export_excel(table_name):
    host,user,passwd,db='192.168.1.152','root','myjcyf','us_sys'
    coon=pymysql.connect(host=host,user=user,passwd=passwd,db=db,port=32781,charset='utf8')
    cur=coon.cursor() #建立游标
    sql='select * from %s;'%table_name
    cur.execute(sql)#执行sql
    fileds=[filed[0] for filed in cur.description]#所有字段
    all_data=cur.fetchall()#所有值
    print(all_data)
    book=xlwt.Workbook()
    sheet=book.add_sheet('sheet1')
    for col,filed in enumerate(fileds):
        sheet.write(0,col,filed)
    row = 1
    for data in all_data:
        for index, datacol in enumerate(data):  # 控制列
            sheet.write(row, index, datacol)
        row = row+ 1
    book.save('%s.xls' % table_name)
export_excel('us_sys.t_dw_bzzx_sbfl')  # 导出excel

  

标签:sheet,cur,行和列,excel,导出,循环,filed,data,row
来源: https://www.cnblogs.com/ruijie/p/14958752.html