from io import BytesIO
from django.utils.encoding import escape_uri_path
bytes_file = BytesIO()
f.write(bytes_file)
bytes_file.seek(0)
file_name = "订单汇总.xls"
response = StreamingHttpResponse(bytes_file)
response['Content-Type'] = 'application/vnd.ms-excel'
response['Content-Disposition'] = 'attachment;filename={}'.format(escape_uri_path(file_name))
# bytes_file.close()
return response
resp = unified_response(code=status.RequestErr, message='请求数据不存在')
return Response(resp)
def export_nota_excel(self,request):
site_name = request.GET.get('site_name')
plan_complete = request.GET.get('plan_complete')
creater = request.GET.get('creater')
plan_type = request.GET.get('plan_type')
start_time = request.GET.get('start_time')
end_time = request.GET.get('end_time')
is_type012 = request.GET.get('is_type012')
if not site_name:
resp = unified_response(code=status.RequestErr, message='请求参数基地名称不能为空')
return Response(resp)
sql_filter_order = 'AND o.order_type in (0,1,2)' if is_type012 in ['true', 'True', '1'] else 'AND o.order_type in (0,1)'
sql_order = f"""
SELECT *
FROM orders_order o
INNER JOIN orders_management m
ON o.uid = m.order_id
INNER JOIN users_userinfo u
on o.creater_id = u.uid
WHERE o.site_name = "{site_name}"
{'AND o.plan_complete = "%s"'%plan_complete if plan_complete else ''}
{'AND o.creater_id = "%s"'%creater if creater else ''}
{'AND o.order_type = "%s"'%plan_type if plan_type else sql_filter_order}
{'AND o.create_time >= "%s"'%start_time if start_time else ''}
{'AND o.create_time <= "%s"'%end_time if end_time else ''}
ORDER BY o.create_time;
"""
df_columns = ['plan_complete',
'name',
'create_time',
]
columns_dict = {'plan_complete': '订单状态',
'name': '创建人',
'create_time': '创建时间',
}
key_col = 'Trip#'
merged_cols = ['订单状态',
'创建人',
'创建时间',
]
bytes_file = BytesIO()
order_df = pd.read_sql(sql_order, connection)
for col in df_columns:
if col not in order_df.columns.values:
df_columns.remove(col)
order_df = order_df.loc[:,df_columns]
df_columns_excel = [columns_dict[col] for col in df_columns]
order_df.columns = df_columns_excel
excel_writer = Excel_writer(bytes_file,order_df)
plan_status_dict = {0: '计划中', '1': '已完成'}
for k,v in plan_status_dict.items():
order_df.loc[order_df['订单状态'] == k,'订单状态'] = v
excel_writer.write_merged_by_col_to_excel(key_col,merged_cols)
bytes_file.seek(0)
file_name = "订单汇总.xls"
response = StreamingHttpResponse(bytes_file)
response['Content-Type'] = 'application/vnd.ms-excel'
response['Content-Disposition'] = 'attachment;filename={}'.format(escape_uri_path(file_name))
return response