django excel下載案例

https://github.com/pyexcel/django-excel



http://pythonhosted.org/Flask-Excel/


CSV:

http://www.cnblogs.com/haoshine/p/5695760.html


excel:

http://www.cnblogs.com/junyiningyuan/p/4680998.html



####下載模塊

def file_download(request):

cols = ['日期','總點擊現(xiàn)金']

##? ? cols = request.GET['cols_select']

cols_str = ','.join(cols)

select_sql = "select top 10 %s from original_DG.dbo.消費信息 where 日期>='20170730'"%cols_str

conn = pymssql.connect(server='localhost',database='ZZ_DG',charset="utf8")

cur = conn.cursor()

cur.execute(select_sql)

temp_data = pd.DataFrame(cur.fetchall(),columns=cols)

conn.commit()

conn.close()

data = temp_data.values.tolist()

data.insert(0,temp_data.columns.tolist())

print(data)

##? ? sheet = excel.pe.Sheet(data)

sheet = excel.pe.Sheet(temp_data.values.tolist(),colnames=cols)

return excel.make_response(pyexcel_instance=sheet,file_type="csv",file_name="test")

####下載模塊2

def file_download_test(request):

cols = ['日期','總點擊現(xiàn)金']

cols_str = ','.join(cols)

select_sql = "select top 10 %s from original_DG.dbo.消費信息 where 日期>='20170730'"%cols_str

conn = pymssql.connect(server='localhost',database='ZZ_DG',charset="utf8")

cur = conn.cursor()

cur.execute(select_sql)

temp_data = pd.DataFrame(cur.fetchall(),columns=cols)##將list[tuple]轉(zhuǎn)化為df

descriptions = cur.description

description_list = []

for description in descriptions:

description_list.append(description[0])

conn.commit()

conn.close()

data = temp_data.values.tolist()##將df轉(zhuǎn)化為list[list]

response = HttpResponse(content_type='text/csv')

# 聲明一個csv的響應(yīng)

response['Content-Disposition'] = "attachment; filename='myname.csv'"

# csv的響應(yīng)的編碼格式聲明

response.write(codecs.BOM_UTF8)

writer = csv.writer(response)

writer.writerow(description_list)##寫入列名

for result in data:

writer.writerow(result)

response.close()

return response

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容