try:
# 下载excel
if request.GET.get('excel') == 'true':
output = BytesIO()
workbook = xlsxwriter.Workbook(output)
sheet = workbook.add_worksheet('开店数据明细表')
# sheet.write(0, 0, "评测结果报表") # 0,0 第一表格写入数据
# sheet.merge_range('A1:C1', '评测结果报表') # 设置合并单元格
# sheet.set_row(0, 20) # 设置第1行的高度
sheet.set_column("A:A", 20) # 设置第1列的宽度
sheet.set_column("B:B", 20) # 设置第2列的宽度
sheet.set_column("C:C", 20) # 设置第3列的宽度
sheet.set_column("F:F", 20) # 设置第6列的宽度
sheet.set_column("G:G", 20) # 设置第7列的宽度
sheet.set_column("H:H", 12) # 设置第8列的宽度
#或者
sheet.col(0).width = 256 * 20 # 设置第1列的宽度
sheet.col(1).width = 256 * 20 # 设置第2列的宽度
sheet.col(2).width = 256 * 20 # 设置第3列的宽度
sheet.col(3).width = 256 * 20 # 设置第4列的宽度
sheet.col(4).width = 256 * 20 # 设置第5列的宽度
sheet.col(5).width = 256 * 20 # 设置第6列的宽度
sheet.col(6).width = 256 * 20 # 设置第7列的宽度
headers = ('用户昵称', '来源场景', '开店页面停留时长', '开店状态', '开店结果', '访问时间','访问页面路径','页面访问时间')
for i, header in enumerate(headers):
sheet.write(0, i, header)
row = 1
for i, item in enumerate(creatshop_datas):
sheet.write(row, 0, item.user_name)
sheet.write(row, 1, item.scene_real)
sheet.write(row, 2, '%s秒' % item.duration)
sheet.write(row, 3, item.status_real)
sheet.write(row, 4, item.result)
sheet.write(row, 5, item.create_time.strftime('%Y-%m-%d %H:%M:%S'))
if item.pages:
page_num = 6
for i,page in enumerate(item.pages):
time_num = page_num+1
sheet.write(row, page_num, page.new_page_name)
sheet.write(row, time_num, '%s秒'%page.duration)
page_num = time_num+1
row += 1
file_name = "开店数据明细%s.xls" % str(date_str)
workbook.close()
xlsx_data = output.getvalue()
response = HttpResponse(content_type='application/ms-excel')
response['Content-Disposition'] = 'attachment; filename=%s' % file_name
response.write(xlsx_data)
logger.info("报表下载成功")
return response
except Exception as e:
logger.info(e)