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 = ['日期','总点击现金']
## 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 = ['日期','总点击现金']
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]转化为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转化为list[list]
response = HttpResponse(content_type='text/csv')
# 声明一个csv的响应
response['Content-Disposition'] = "attachment; filename='myname.csv'"
# csv的响应的编码格式声明
response.write(codecs.BOM_UTF8)
writer = csv.writer(response)
writer.writerow(description_list)##写入列名
for result in data:
writer.writerow(result)
response.close()
return response