zoukankan      html  css  js  c++  java
  • 把数据库里面的stu表中的数据,导出到excel中

    # 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')
  • 相关阅读:
    DateUtils
    Java静态绑定与动态绑定
    Mysql中实现递归查询
    架构一、核心概念
    Spring cron 表达式
    MySql点点滴滴(一)之可视化工具介绍
    java中注解的使用与实例
    3、第一个Python程序
    CSS命名
    如何在Notepad++ 中成功地安装Emmet 插件
  • 原文地址:https://www.cnblogs.com/jiadan/p/9033446.html
Copyright © 2011-2022 走看看