python,Django,ajax 导出excel


<input type="submit" class="btn btn-success" onclick="exportInfo()" value="导出">
function exportInfo() {
        alert($('#queryInfo').serialize());
        url = "/registration/downloadInfo/?"+$('#queryInfo').serialize()
        $(location).attr('href', url);
}
import xlwt
def downloadInfo(request):
    # 通过get方法获取页面上的审核意见
    # print(request.GET)
    breeds_code = request.GET.get('breeds_code')
    sex = request.GET.get('sex')
    identify_date = request.GET.get('identify_date')
    horseowner = request.GET.get('horseowner')
    burg_is_pass = request.GET.get('burg_is_pass')
    is_update = request.GET.get('is_update')
    issue_date = request.GET.get('issue_date')
    # print(breeds_code)
    queryset = passport.objects.all()
    if breeds_code:
        queryset = queryset.filter(horse__breeds_code_id=breeds_code)
    if sex:
        queryset = queryset.filter(horse__sex=sex)
    if identify_date:
        queryset = queryset.filter(horse__identify_date__year=identify_date)
    if horseowner:
        queryset = queryset.filter(horse__horseowner__user__userName__icontains=horseowner)
    if burg_is_pass:
        queryset = queryset.filter(burg_is_pass=burg_is_pass)
    if is_update:
        queryset = queryset.filter(is_update=is_update)
    if issue_date:
        queryset = queryset.filter(issue_date=issue_date)
    print(queryset.count())

    book = xlwt.Workbook(encoding='utf-8', style_compression=0)
    sheet = book.add_sheet('test', cell_overwrite_ok=True)
    row0 = ['RFID', '马名', '性别', '品种', '登记日期', '生成日期', '进度','马主','马主电话','邮寄联系人','邮寄电话','邮寄地址']
    for i in range(0,len(row0)):
        sheet.write(0, i, row0[i])
    all_ls = []
    for j in range(len(queryset)):
        ls = []
        ls.append(queryset[j].horse.rfid)
        ls.append(queryset[j].horse.chinese_name)

        sex = ""
        if queryset[j].horse.sex:
            if queryset[j].horse.sex =="male":
                sex = "公"
            elif queryset[j].horse.sex == "female":
                sex = "母"
            else:
                sex = "骟"
        ls.append(sex)

        ls.append(queryset[j].horse.breeds_code.breeds_name)

        identify_date = str(queryset[j].horse.identify_date)
        ls.append(identify_date)

        issue_date = str(queryset[j].issue_date)
        ls.append(issue_date)

        burg_is_pass = ""
        if queryset[j].burg_is_pass == True:
            burg_is_pass = "已生成"
        else:
            burg_is_pass = "未生成"
        ls.append(burg_is_pass)
        ls.append(queryset[j].horse.horseowner.user.userName)
        ls.append(queryset[j].horse.horseowner.user.mobile)
        ls.append(queryset[j].addressee)
        ls.append(queryset[j].addressee_phone)
        ls.append(queryset[j].address)
        all_ls.append(ls)
    # print(all_ls)
    response = HttpResponse(content_type='application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; filename="excel.xls"'


    for r in range(0,len(all_ls)):
        for c in range(len(row0)):
            sheet.write(r+1, c, all_ls[r][c])
            # print(all_ls[r][c])

    buffer = BytesIO()
    book.save(buffer)
    buffer.seek(0)
    # excel = buffer.getvalue()
    response.write(buffer.getvalue())
    buffer.close()
    # book.save('test.xls')  # 保存本地xls
    # return JsonResponse({'status':"SUCCESS"},safe=False)
    return response


你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答


本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。


因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。