Python GAE, Django Export Excel method _python

Source: Internet
Author: User
But Gae, Django does not export pyexcelerator directly to Excel as a method. My idea is to first import data into workbook and worksheet, if the save as a file can directly call the workbook save method, but Gae does not support local file operations, even if the picture can only be stored in the datastore, But we can sort of return to the image by returning the binary stream of Excel directly to the browser. This need to modify the workbook code, add a way to return to the binary stream, I gave him the name is Savestream, in the Savestream again call Compounddoc.xlsdoc Savestream method, but also increase their own. The code is as follows:
Workbook's Savestream:
Copy Code code as follows:

def savestream (self):
Import Compounddoc
doc = Compounddoc.xlsdoc ()
Return Doc.savestream (Self.get_biff_data ())

the Savestream method of Compounddoc.xlsdoc:
Copy Code code as follows:

def savestream (self, stream):
# 1. Align Stream on 0x1000 boundary (and therefore on sector boundary)
padding = ' \x00 ' * (0x1000-(len (stream)% 0x1000))
Self.book_stream_len = Len (stream) + len (padding)
Self.__build_directory ()
Self.__build_sat ()
Self.__build_header ()
s = ""
s = s + str (self.header)
s = s + str (self.packed_msat_1st)
s = s + str (stream)
s = s + str (padding)
s = s + str (self.packed_msat_2nd)
s = s + str (SELF.PACKED_SAT)
s = s + str (self.dir_stream)
return s

This allows you to return to the Excel file binary stream, the following is how the user requests to return the Excel file, I borrowed the PHP implementation method, the code is as follows:
Copy Code code as follows:

Class Main (WebApp. RequestHandler):
def get (self):
Self.sess = Session. Session ()
t_values[' user_id '] = self.sess[' userid ']
If Self.request.get (' export ') = = ' Excel ':
WB = Workbook ()
WS = Wb.add_sheet (U ' Statistic report ')
#表头
Font0 = Font ()
Font0.bold = True
Font0.height = 12*20;
Styletitle = Xfstyle ()
Styletitle.font = font0
Ws.write (0, 0, u "Date:" +begintime.strftime ('%y-%m-%d ') + "-" + endtime.strftime ('%y-%m-%d '), Styletitle)
#返回Excel文件
self.response.headers[' content-type ' = "application/vnd.ms-execl"
self.response.headers[' content-disposition ' = str ("attachment; Filename=%s.xls "%t_values[' user_id '])
self.response.headers[' Pragma ' = "No-cache"
self.response.headers[' Expires '] = "0"
Self.response.out.write (Wb.savestream ())
Return

The effect can be seen in my love of the billing network Excel report.
Related Article

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.