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