将表数据写到excel

#导出excel表格数据
def datainput(request):
    type = request.GET.get('activity','')
    key = request.GET.get('gjz','')
    timereport =request.GET.get('dateTime','')
    if timereport != '':
        a = timereport[:10]
        b = timereport[-10:]+'/23/59/59'
        c = datetime.strptime(a, '%Y-%m-%d')
        d = datetime.strptime(b,'%Y-%m-%d/%H/%M/%S')
        if type == '0':
            if key != '':
                activity_list=Order.objects.filter(activity_id__start_date__gte=c).filter(activity_id__start_date__lte=d).filter(activity_id__name__contains=key).values_list( 'activity_id__name','name','start_time','end_time',
                                             'report_order__zhibo_click','report_order__zhibo_read','report_order__zhibo_call',
                                           'activity_id__start_date','activity_id__end_date', 'activity_id__advertiser_name')
            else:
                activity_list=Order.objects.filter(activity_id__start_date__gte=c).filter(activity_id__start_date__lte=d).values_list('activity_id__name','name','start_time','end_time',
                                             'report_order__zhibo_click','report_order__zhibo_read','report_order__zhibo_call',
                                             'activity_id__start_date','activity_id__end_date','activity_id__advertiser_name')
        else:
            if key != '':
                activity_list = Order.objects.filter(activity_id__start_date__gte=c).filter(activity_id__start_date__lte=d).filter(activity_id__id__contains=key).values_list( 'activity_id__name','name','start_time','end_time',
                                             'report_order__zhibo_click','report_order__zhibo_read','report_order__zhibo_call',
                                           'activity_id__start_date','activity_id__end_date', 'activity_id__advertiser_name')
            else:
                activity_list =Order.objects.filter(activity_id__start_date__gte=c).filter(activity_id__start_date__lte=d).values_list( 'activity_id__name','name','start_time','end_time',
                                             'report_order__zhibo_click','report_order__zhibo_read','report_order__zhibo_call',
                                          'activity_id__start_date','activity_id__end_date',  'activity_id__advertiser_name')

    response = HttpResponse(content_type='application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; filename=reportdata.xls'  # 返回下载文件的名称(activity.xls)
    workbook = xlwt.Workbook(encoding='utf-8')  # 创建工作簿
    mysheet = workbook.add_sheet(u'活动')  # 创建工作页
    rows=activity_list
    cols = 10#每行的列
    aaa = ['活动名称','订单名称','投放日期','结束日期','直播点击数','直播订阅量','直播访问量','开始时间','结束时间','广告主名称']  #表头名
    for c in range(len(aaa)):
        mysheet.write(0,c,aaa[c])
    for r in range(0, len(rows)):      #对行进行遍历
        for c in range(cols):          #对列进行遍历
            mysheet.write(r+1,c,str(rows[r][c]))
            response = HttpResponse(
                content_type='application/vnd.ms-excel')  # 这里响应对象获得了一个特殊的mime类型,告诉浏览器这是个excel文件不是html
            response[
                'Content-Disposition'] = 'attachment; filename=reportdata.xls'  # 这里响应对象获得了附加的Content-Disposition协议头,它含有excel文件的名称,文件名随意,当浏览器访问它时,会以"另存为"对话框中使用它.
            workbook.save(response)
    return response
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值