Using Python to generate Excel files on a Web page

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.