#导出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
将表数据写到excel
最新推荐文章于 2024-07-26 16:48:19 发布