码迷,mamicode.com
首页 > 其他好文 > 详细

django导出数据到excel

时间:2017-05-27 13:37:05      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:port   name   dump   ice   att   add   filename   取数据   use   

import xlwt,StringIo
def
dumpData(request): #获取数据 activitys = Activity.objects.all().order_by(id) if activitys: #创建workbook对象并设置编码 ws = xlwt.Workbook(encoding=utf-8) w = ws.add_sheet(u评论完成,cell_overwrite_ok=True) w.write(0, 0, u"订单编号") w.write(0, 1, u"买家") w.write(0, 2, u"购买时间") w.write(0, 3, u"付款金额") w.write(0, 4, u"提交订单号") w.write(0, 5, u"返现金额") w.write(0, 6, u"状态") excel_row = 1 for activity in activitys: id = activity.id buyer = activity.buyer.buyer.username time = activity.time price = activity.pay orderId = activity.orderId payBack = activity.payBack status = activity.status w.write(excel_row, 0, id) w.write(excel_row, 0, buyer) w.write(excel_row, 0, time) w.write(excel_row, 0, price) w.write(excel_row, 0, orderId) w.write(excel_row, 0, payBack) w.write(excel_row, 0, status) excel_row += 1 exist_file = os.path.exists(test.xls) if exist_file: os.remove(rtest.xls) ws.save(test.xls) #返回文件给客户 sio = StringIO.StringIO() ws.save(sio) sio.seek(0) response = HttpResponse(sio.getvalue(), content_type=application/vnd.ms-excel) response[Content-Disposition] = attachment; filename=test.xls response.write(sio.getvalue()) return response

 

django导出数据到excel

标签:port   name   dump   ice   att   add   filename   取数据   use   

原文地址:http://www.cnblogs.com/lgh344902118/p/6912193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!