2017-03-20 2 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', 
}     

2 단계 (단지 파일처럼 당신은 /web/controllers/main.py에서 볼 수) : 컨트롤러 클래스를 확인하고 해당 URL을 잡아 파일을 엑셀 다운로드 과정을.

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 = %의 & 파일 이름에서

아래를 참조, 나는 통과 한 모델, 필드, ID와 이름 = product_stock.xls

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

모든 파일이 txt, 당신은 XLS, CSV를 만들 수 있습니다 방법보다 사용하기.

감사합니다.

+0

답변에 링크 만 게시하면 안됩니다. 링크가 더 이상 유효하지 않으면 어떻게됩니까? 이것은 차례 차례로 당신의 답을 무효로 만들 것입니다. – Goralight

+1

좋아, 다음에 나는 적절한 예를 들어 대답 할 것입니다. –

관련 문제