关于excel导出下载

from io import BytesIO
def xls_export(data_list, file_name):
    """
    data_list 类型为list
    """
    out_put = BytesIO()
    wb = xlsxwriter.Workbook(out_put)
    ws = wb.add_worksheet(file_name)
    row = 0
    for lis in data_list:
        cel = 0
        for item in lis:
            ws.write(row, cel, convertStr(item))
            cel += 1
        row += 1
    wb.close()
# 
    out_put.seek(0)
# 指定文件类型
    response = HttpResponse(content_type="application/ms-excel")
    filename_data = {'a': file_name}
    fileName = urlencode(filename_data)
# 文件name
    response['Content-Disposition'] = ('attachment; filename=%s.xlsx' % fileName.split('=')[1])
    response.write(out_put.getvalue())
    return response

你可能感兴趣的:(关于excel导出下载)