python automatically generated excel (xlwt Library)

The following code uses web.py frame, the other frame are similar.

Coding #: UTF- . 8 

Import Web 
Import JSON 
Import datetime 
Import xlwt 
Import the StringIO 

# call interface if not the front end, the frame and URLs as web.py app, render possible to omit 
URLs = (# web routing framework
     ' / index ' , ' Index ' ,
     ' / Home ' , ' Home ' , 
) 
App = web.aplication (urls, Globals ()) # registration application 
the render = web.template.render ( ' template ' ) # template, note the path 

# If you do not call the front interfaces, as URLs and app web.py frame, render may be omitted

# This class, only a return to the front page and inside pages of a button using ajax to request the following additional interfaces, this I do not write html content 
class Index ( Object ): 
    DEF GET (Self): 
        return render.mubanmingzi ({}) 

# generate Excel file 
class Excel ( Object ): 
    DEF GET (Self): 
        web.header ( ' content-of the type ' , ' applicationvnd.ms-Excel ' ) # request header, returns the specified content 
        web.header ( ' Transfer-Encoding ' , ' chunked ' ) 
        namedate = str (datetime.date.today ()) 
# set the user's browser generates a file name excel web.header (
'Disposition-the Content ' , ' Attachment: FILNAME = {0} ' .format ( ' Excel table ' + + namedate ' XLS ' ) # above is not important, for reference, is used as a third-party library xlwt python generating xecel file wb = xlwt.Workbook (encoding = ' UTF-8 ' ) # create a workbook object WS = wb.add_sheet ( ' sheet ' ) # name to be written sheet, is excel table, do not understand can go Baidu BS = xlwt.Borders () # create the border object table is used to specify the style of the border (thickness, to achieve a broken line, color, etc.) bs.left = xlwt.Borders.THIN left solid line # bs.right = xlwt.Borders.THIN # right solid line bs.top = xlwt.Borders.THIN # upper solid line bs.bottom = xlwt.Boeders.THIN lower solid line # bs.left_colour = OX40 # color style = xlwt.XFStyle () # create a style object style.borders = bs bs # will set a good style to style objects # below began to excel inside the created object is written to the data # 0 line we will be 5 before merging centered lattice, used to write precautions
before two # 0 represents row 0 to row 0 from this range, 0 represents the first three columns from 0, 5 to column 4 indicates (row indexes from 0 start), the last value of the content is to be written) ws.write_merge (
0 , 0 , 0 , . 4 , 'This is written content ' )
# header provided
for I in Range ( . 5 ): ws.write ( 1 , I, ' title 0} { ' .format (STR (I))) # grid. 5, 1 denotes two lines, i denotes the first 5 cells of the second row, the title 0- title 4 do not each line is # below Similarly, writes to row = 2 # third row start writing the content data for I in Range ( 10 ): # 10 is written row ws.write (row, 0 , ' the contents. 1 ' , style) # contents are written, border style commencement ws.write (row, . 1 , 'SUMMARY OF 2 ' , style) ws.write (Row, 2 , ' content. 3 ' , style) ws.write (Row, . 3 , ' content. 4 ' , style) ws.write (Row, . 4 , ' content. 5 ' , style ) row + = . 1 # specify the width of each column ws.col ( 0 ) = .width 3999 # 0-th column ws.col ( . 1 ) .width = 3999 # column. 1 ws.col ( . 4 ) = .width 6999 # data streams saved to local disk SiO = StringIO.StringIO () # create a data stream web.save (sio) # note here is the preservation of the data stream, not a file name sio.seek ( 0 ) # cursor, start from position 0 is written return sio.getvalue () # will generate special effects file back to the browser, in fact, the file has been saved to a computer to download the file.

 

Guess you like

Origin www.cnblogs.com/aaronthon/p/11609837.html