Python导出数据到excel

from trump.query import  create_item,modify_item,get_items
from utils.result_process import success,aborted
import xlwt
import datetime
from utils.data_to_execl_sunge import excel_style
import config_business


# 统计用户登录次数
async def ls(app,request):
    params = request.args
    pager = False
    with_total = False
    if "page" in params:  # 判断是否有分页
        pager = True
        with_total = True
    re_params = {}
    for i in params:  # 重新封装传入参数
        re_params.__setitem__(i, params.get(i))
    data = await get_items(app.pool, "users_log", re_params, with_total=with_total, pager=pager)
    print(data)
    if data:
        if pager == True:  # 有分页
            if "excel" in request.args and request.args.get("excel") == "daochu":
                today_date = datetime.datetime.now().strftime("%Y-%m-%d_%H:%M:%S")
                wbk = xlwt.Workbook()
                sheet = wbk.add_sheet('Sheet1', cell_overwrite_ok=True)
                header01, style_COL_AC = excel_style()
                head_message = ['编号', '手机号', '最近登录ip地址','最近登录时间','登录次数']
                tall_style = xlwt.easyxf('font:height 600;')
                for kk, i in enumerate(head_message):
                    sheet.write(0, kk, i, header01)
                    sheet.col(kk).width = 240 * 20
                    sheet.row(0).set_style(tall_style)
                for key, item in enumerate(data[1]):
                    for k, ele in enumerate(item):
                        sheet.write(int(key + 1), k, item[ele], style_COL_AC)
                        sheet.col(k).width = 240 * 20
                        sheet.row(int(key + 1)).set_style(tall_style)
                excel_path = config_business.EXCEL_SAVE_PATH + str(today_date) + '用户登录次数' + '.xls'
                wbk.save(excel_path)
                return success(excel_path)
            return success({"total": data[0], "list": data[1]})
        else:
            return success(data)
    else:
        return aborted({"total": 0, "list": []})

你可能感兴趣的:(Python导出数据到excel)