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:Yimeijian@163.cn@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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM