转载自:http://www.sharejs.com/codes/python/7265
import xlwt
import StringIO
import web
urls = (
'/rim_request','rim_request',
'/rim_export','rim_export',
'/(.*)', 'index'
)
class rim_export:
#render = web.template.render('adsl')
def GET(self):
web.header('Content-type','application/vnd.ms-excel') #指定返回的类型
web.header('Transfer-Encoding','chunked')
web.header('Content-Disposition','attachment;filename="export.xls"') #设定用户浏览器显示的保存文件名
wb=xlwt.Workbook()
wb.encoding='gbk'
ws=wb.add_sheet('1')
ws.write(0,1,'123') #如果要写中文请使用UNICODE
sio=StringIO.StringIO()
wb.save(sio) #这点很重要,传给save函数的不是保存文件名,而是一个StringIO流
return sio.getvalue()