Dec 15, 2008
pyExcelerator django 创建可供下载的 excel
pyExcelerator 创建excel的代码大概如下:
from pyExcelerator import *
wb = Workbook()
ws = w.add_sheet('Sheet1')
ws.set_show_headers = 0
header = ['姓名', '手机号码', '商品名称', '售出数量', '单价(RMB)', '日期']
header = [x.decode('utf8') for x in header]
for i in range(len(header)):
ws.write(0, i, header[i])
wb.save()
在django中提供下载:
from pyExcelerator import *
xlsname = 'test.xls'
w = Workbook()
ws = w.add_sheet('Sheet1')
ws.set_show_headers = 0
header = ['姓名', '手机号码', '商品名称', '售出数量', '单价(RMB)', '日期']
header = [x.decode('utf8') for x in header]
for i in range(len(header)):
ws.write(0, i, header[i])
w.save(xlsname)
response = HttpResponse(mimetype='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment;filename=%s' %xlsname
response.write(w.savestream())
return response
注意,Workbook类中是没有savestream方法的,参考http://blog.csdn.net/kernelspirit/archive/2008/10/26/3147888.aspx
修改pyExcelerator代码
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
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
您的评论将使我blog更有动力~