- 查询数据生成excel
- 返回给前端
import openpyxl
import StringIO
from django.utils.encoding import escape_uri_path
def downexcel(request):
wb = openpyxl.Workbook()
ws = wb.active
ws.title=u'测试表名'
ws.merge_cells('A1:P1')
ws['A1'] = '这是用来测试的标题'
sio = StringIO.StringIO()
wb.save(sio)
sio.seek(0)
res = sio.getvalue()
response = HttpResponse(res, content_type='application/vnd.ms-excel', )
response['Content-Disposition'] = "attachment; filename=%s.xls" % escape_uri_path('测试文件名'.encode('utf-8'))
return response