用python将数据库数据查询导出excel表,多个表单

效果如下:

excel底部表单列表

 

# -*- coding: utf-8 -*-

import pymysql
import xlwt

cy_greenskin = {
    'host': '10.xx.xx.89',
    'port': 3306,
    'user': 'cmt',
    'passwd': 'cmtxxx',
    'cursorclass': pymysql.cursors.DictCursor,
    'db': 'greenskin'
}

cy_cop_isv = {
    'host': '10.xx.xx.13',
    'port': 3306,
    'user': 'cmt',
    'passwd': 'cmtxxx',
    'charset': 'utf8',
    'db': 'cop_isv'
}

kuaizhan_user_config = {
    'host': '10.xx.xx.49',
    'port': 3306,
    'user': 'user',
    'passwd': 'xxx',
    'charset': 'utf8',
    'cursorclass': pymysql.cursors.DictCursor,
    'db': 'kuaizhan_user'
}

cy_greenskin_db = pymysql.connect(**cy_greenskin)
cy_greenskin_cursor = cy_greenskin_db.cursor()

cy_cop_db = pymysql.connect(**cy_cop_isv)
cy_cop_cursor = cy_cop_db.cursor()

kuaizhan_user_db = pymysql.connect(**kuaizhan_user_config)
kuaizhan_user_cursor = kuaizhan_user_db.cursor()


def export_cy_pc_pv(ws, dbname, date):
    sql = "select isv_app_id, isv_name, isv_url, isv_app_id, pv, uv from {} where date = '{}' ".format(dbname, date)

    cy_greenskin_cursor.execute(sql)
    cy_greenskines = cy_greenskin_cursor.fetchall()
    count = 1
    pv = 0
    uv = 0
    for cy_greenskin in cy_greenskines:
        sql = "select gmt_vip_expire from isv left join vip_isv vi on isv.isv_id = vi.isv_id where isv.isv_app_id = '{}'".format(cy_greenskin['isv_app_id'])
        cy_cop_cursor.execute(sql)
        cy_cop = cy_cop_cursor.fetchone()

        pv = pv + cy_greenskin['pv']
        uv = uv + cy_greenskin['uv']

        if cy_cop[0] is None:
            ws.write(count, 5, '否')
        else:
            ws.write(count, 5, '是')

        ws.write(count, 0, cy_greenskin['isv_name'])
        ws.write(count, 1, cy_greenskin['isv_url'])
        ws.write(count, 2, cy_greenskin['isv_app_id'])
        ws.write(count, 3, cy_greenskin['pv'])
        ws.write(count, 4, cy_greenskin['uv'])
        ws.write
        count = count + 1
    ws.write(1, 6, pv)
    ws.write(1, 7, uv)


def get_ws(wb, name):
    ws = wb.add_sheet(name)  # 设置工作表名称
    ws.write(0, 0, u'名称')  # 向表格中插入字符串,前两位数字分别为行和列,第三个参数为要插入的内容,第四个参数可以设置样式
    ws.write(0, 1, u'url')
    ws.write(0, 2, u'app_id')
    ws.write(0, 3, u'pv')
    ws.write(0, 4, u'uv')
    ws.write(0, 5, u'vip')
    ws.write(0, 6, u'pv总量')
    ws.write(0, 7, u'uv总量')
    return ws

if __name__ == '__main__':
    wb = xlwt.Workbook(encoding='ascii')  # 创建实例,并且规定编码
    # ws = wb.add_sheet("PC端数据")  # 设置工作表名称
    # ws.write(0, 0, u'名称')  # 向表格中插入字符串,前两位数字分别为行和列,第三个参数为要插入的内容,第四个参数可以设置样式
    # ws.write(0, 1, u'url')
    # ws.write(0, 2, u'app_id')
    # ws.write(0, 3, u'pv')
    # ws.write(0, 4, u'uv')
    # ws.write(0, 5, u'vip')
    # ws.write(0, 6, u'pv总量')
    # ws.write(0, 7, u'uv总量')
    ws1 = get_ws(wb, "PC端数据0403")
    export_cy_pc_pv(ws1, 'isv_stat', '20190403')

    ws2 = get_ws(wb, 'wap端数据0403')
    export_cy_pc_pv(ws2, 'mobile_stat', '20190403')

    ws3 = get_ws(wb, 'ios数据0403')
    export_cy_pc_pv(ws3, 'ios_stat_api', '20190403')

    ws4 = get_ws(wb, 'android数据0403')
    export_cy_pc_pv(ws4, 'android_stat_api', '20190403')

    ws5 = get_ws(wb, "PC端数据1104")
    export_cy_pc_pv(ws5, 'isv_stat', '20191104')

    ws6 = get_ws(wb, 'wap端数据1104')
    export_cy_pc_pv(ws6, 'mobile_stat', '20191104')

    ws7 = get_ws(wb, 'ios数据1104')
    export_cy_pc_pv(ws7, 'ios_stat_api', '20191104')

    ws8 = get_ws(wb, 'android数据1104')
    export_cy_pc_pv(ws8, 'android_stat_api', '20191104')


    wb.save(u'xx数据.xls')  # 将生成的表格保存为

    # ws2 = wb.add_sheet()
发布了30 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_40898368/article/details/103093305