从odoo中导出Excel

利用xlwt生成Excel,与生成文件不同的是跳转到URL进行下载。

1
2
3
4
5
6
7
8
9
sio = StringIO.StringIO()
wkbook.save(sio)
records[0].xls = sio.getvalue()
return {
'type': 'ir.actions.act_url',
'url': '/web/binary/download_document?model=shop.reg&field=xls&id=%s&filename=%s.xls' % (records[0].id, records[0].shop_name),
'target': 'self',
}

然后在controller中返回正常的respone:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@http.route('/web/binary/download_document', type='http', auth="public")
@serialize_exception
def download_document(self,model,field,id,filename=None, **kw):
""" Download link for files stored as binary fields.
:param str model: name of the model to fetch the binary from
:param str field: binary field
:param str id: id of the record from which to fetch the binary
:param str filename: field holding the file's name, if any
:returns: :class:`werkzeug.wrappers.Response`
"""
export = request.env[model].sudo().browse(int(id))
filecontent = export.xls
if not filecontent:
return request.not_found()
else:
if not filename:
filename = '%s_%s' % (model.replace('.', '_'), id)
return request.make_response(filecontent,
[('Content-Type', 'application/octet-stream'),
('Content-Disposition', content_disposition(filename))])