[web.py-REST开发之路]web.py生成excel文件并下载

talk is cheap,show you the code

*需要安装xlwt


import xlwt
import web
def download_report(query_set)
	file_name="test.xls"
	"""设置web header"""
        web.header('Content-type','application/vnd.ms-excel')  #指定返回的类型
        web.header('Transfer-Encoding','chunked')
        web.header('Content-Disposition','attachment;filename="%s"'%file_name) #设定用户浏览器显示的保存文件名
        """开始制作表格"""
        wb=xlwt.Workbook()
        wb.encoding='utf8'	#设置字符编码
        ws=wb.add_sheet('1')	
        """表头开始"""
        ws.write(0,0,u'序号')
	ws.write(0,1,u'时间')
	
	"""设置宽度""" 
	col=ws.col(0) 
	col.width=256*15 #15个字符宽度
	"""表体开始"""
	
	count=1 
	for item in query_set:
            if item.get('create_user') is not None:
                ws.write(count,0,count)
                ws.write(count,1,str(item.get('create_date')))
                count+=1

        sio=StringIO.StringIO()
        wb.save(sio)
        return sio.getvalue()






  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值