Python GAE、Django導(dǎo)出Excel的方法
更新時(shí)間:2008年11月24日 20:41:34 作者:
在Python中操作Excel的方法可以通過(guò)COM,最常用的跨平臺(tái)的方法是使用pyExcelerator,pyExcelerator的使用方法可以參考limodou的《使用pyExcelerator來(lái)讀寫Excel文件》。
但GAE、Django并沒有直接將pyExcelerator導(dǎo)出為Excel的方法。我的思路是先用把數(shù)據(jù)導(dǎo)入到Workbook和Worksheet中,如果存為文件可以直接調(diào)用Workbook的save方法,但GAE不支持本地文件操作,即使圖片也只能存放在DataStore中,但我們可以類似于返回圖片的方法,直接將Excel的二進(jìn)制流返回給瀏覽器。這就需要修改一下Workbook的代碼,加入返回二進(jìn)制流的方法,我給他取的名字是savestream,在savestream中再次調(diào)用CompoundDoc.XlsDoc的savestream方法,也是自己增加的。代碼如下:
Workbook的savestream:
def savestream(self):
import CompoundDoc
doc = CompoundDoc.XlsDoc()
return doc.savestream(self.get_biff_data())
CompoundDoc.XlsDoc的savestream方法:
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
這樣就可以返回Excel文件的二進(jìn)制流了,下面就是如何在用戶請(qǐng)求的時(shí)候?qū)xcel文件返回,我借鑒了PHP的實(shí)現(xiàn)方法,代碼如下:
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'統(tǒng)計(jì)報(bào)表')
#表頭
font0 = Font()
font0.bold = True
font0.height = 12*20;
styletitle = XFStyle()
styletitle.font = font0
ws.write(0, 0, u"日期:"+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
效果可以參見我愛記賬網(wǎng)的excel報(bào)表。
Workbook的savestream:
復(fù)制代碼 代碼如下:
def savestream(self):
import CompoundDoc
doc = CompoundDoc.XlsDoc()
return doc.savestream(self.get_biff_data())
CompoundDoc.XlsDoc的savestream方法:
復(fù)制代碼 代碼如下:
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
這樣就可以返回Excel文件的二進(jìn)制流了,下面就是如何在用戶請(qǐng)求的時(shí)候?qū)xcel文件返回,我借鑒了PHP的實(shí)現(xiàn)方法,代碼如下:
復(fù)制代碼 代碼如下:
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'統(tǒng)計(jì)報(bào)表')
#表頭
font0 = Font()
font0.bold = True
font0.height = 12*20;
styletitle = XFStyle()
styletitle.font = font0
ws.write(0, 0, u"日期:"+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
效果可以參見我愛記賬網(wǎng)的excel報(bào)表。
相關(guān)文章
Python利用OpenCV和skimage實(shí)現(xiàn)圖像邊緣檢測(cè)
提取圖片的邊緣信息是底層數(shù)字圖像處理的基本任務(wù)之一。本文將通過(guò)OpenCV和skimage的?Canny?算法實(shí)現(xiàn)圖像邊緣檢測(cè),感興趣的可以了解一下2022-12-12
Python中關(guān)于函數(shù)的具體用法范例以及介紹
函數(shù)是組織好的,可重復(fù)使用的,用來(lái)實(shí)現(xiàn)單一,或相關(guān)聯(lián)功能的代碼段。函數(shù)能提高應(yīng)用的模塊性,和代碼的重復(fù)利用率。你已經(jīng)知道Python提供了許多內(nèi)建函數(shù),比如print()。但你也可以自己創(chuàng)建函數(shù),這被叫做用戶自定義函數(shù)2021-09-09
Python嵌套函數(shù),作用域與偏函數(shù)用法實(shí)例分析
這篇文章主要介紹了Python嵌套函數(shù),作用域與偏函數(shù)用法,結(jié)合實(shí)例形式分析了Python嵌套函數(shù),作用域與偏函數(shù)的功能、定義與相關(guān)使用方法,需要的朋友可以參考下2019-12-12
python的staticmethod與classmethod實(shí)現(xiàn)實(shí)例代碼
這篇文章主要介紹了python的staticmethod與classmethod實(shí)現(xiàn)實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
Pygame?精準(zhǔn)檢測(cè)圖像碰撞的問(wèn)題
這篇文章主要介紹了Pygame?精準(zhǔn)檢測(cè)圖像碰撞,在用Pygame寫游戲的時(shí)候,有人可能會(huì)遇到兩個(gè)Rect對(duì)象碰撞但是對(duì)象之間還有空間間隔的問(wèn)題,這里,將教大家用一種方法精準(zhǔn)地檢測(cè)圖像碰撞,需要的朋友可以參考下2022-06-06
python實(shí)現(xiàn)學(xué)生管理系統(tǒng)源碼
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)學(xué)生管理系統(tǒng)源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04

