From: http://blog.sina.com.cn/s/blog_5d18f85f0101bxo7.html
Recently wrote a program needs to generate Excel files on the Web server for users to download, studied a bit to find the following more feasible implementation, the following web.py as an example, the relevant code for your reference:
First, you need to download the module that generates Excel, it is recommended to use XLWT
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) #这点很重要, passed to the Save function is not the save file name, but a Stringio stream
Return Sio.getvalue ()
The benefit of this scenario is that you do not need to generate temporary files to return the Excel file directly to the user's browser as a stream
Generate Excel file garbled problem
' SimSun ' # specifies "arial" style.font = Font table. Write (# use style
Using Python to generate Excel files on a Web page