2017-03-20 33 views
0

詳細的主題我需要一些關於下載xls文件的方式的解釋。 在Odoo8上,通過嚮導創建一個帶有xlwt的xls文件,並使用函數wb.save(filename)將其存儲到文件系統中。 但是,經過大量的谷歌搜索,我不能滿足我的需求,而且我真的很難過...... 有沒有人幫助我正確使用?如何在Odoo8下載xls文件?

回答

0

這裏是完美的例子下載xls文件。

第1步:在常規模型(嚮導)中創建一個方法並返回URL。

@api.multi 
def get_file(self): 
    return { 
      'type' : 'ir.actions.act_url', 
      'url': '/web/binary/download_document?model=wizard.product.stock.report&field=datas&id=%s&filename=product_stock.xls'%(self.id), 
      'target': 'self', 
}     

(就像文件,你可以在/web/controllers/main.py看到)

第2步:做一個控制器類和趕上網址和下載的過程中做excel文件。

from openerp import http 
    from openerp.http import request 
    from openerp.addons.web.controllers.main import serialize_exception,content_disposition 
    import base64 
    class Binary(http.Controller): 
    @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` 
     """ 
     Model = request.registry[model] 
     cr, uid, context = request.cr, request.uid, request.context 
     fields = [field] 
     res = Model.read(cr, uid, [int(id)], fields, context)[0] 
     filecontent = base64.b64decode(res.get(field) or '') 
     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))])  

在上述方法我已得到的URL的ID,然後應用於一些計算,並返回從請求的HTTP響應。無論從嚮導傳遞給控制器​​方法的任何值,我都會將它們應用於控制器方法,並且在該控制器方法中,我將執行必要的過程並直接返回文件。

見下面,我已經過了模型,場,編號和文件名從URL

/web/binary/download_document?model=wizard.product.stock.report &場= DATAS & ID =%s的&文件名= product_stock.xls

http://www.emiprotechnologies.com/technical_notes/odoo-technical-notes-59/post/how-to-download-any-file-on-button-click-244

使用上面的方法,你可以創建XLS,CSV,TXT的文件。

謝謝,

+0

你應該避免只發布鏈接在你的答案。如果鏈接不再有效會發生什麼?這反過來會使你的答案無效... – Goralight

+1

好的,下次我會給你答案與適當的例子。 –