pandas 流式导出excel

1,下载pandas和sqlalchemy

2,开始利用pandas导出

import pandas as pd
from sqlalchemy import create_engine
from io import BytesIO
from django.shortcuts import HttpResponse
outfile = BytesIO()
engine = create_engine('mysql+pymysql://root:[email protected]@106.75.7.128:3306/channel_statistics') 
sql
= 'select name,code,channel_type,pay_type from channel_channel where channel_p_id is not NULL '
df
=pd.read_sql_query(sql,engine) #先读取数据库中的数据

df = df.rename(columns={    #修改数据库中列的名称
'name':'名字',
'code':'refer',
'channel_type':'渠道类型',
'pay_type':'付款类型',
})

response = HttpResponse(content_type='application/vnd.ms-excel')
execl_name = 'test'
response['Content-Disposition'] = 'attachment;filename={0}.xlsx'.format(execl_name)
df.to_excel(outfile,index=False)     #形成流式导出
response.write(by.getvalue())
return response

 

转载于:https://www.cnblogs.com/52forjie/p/10438802.html

你可能感兴趣的:(pandas 流式导出excel)