【问题标题】:How to generate excel report using wizard in Odoo 10, in a single click如何使用 Odoo 10 中的向导生成 Excel 报告,只需单击一下
【发布时间】:2018-06-11 08:57:38
【问题描述】:

我正在尝试使用 Odoo 10 生成 excel 报告。代码运行良好,我能够做到。但是文件不会一键下载。它保存文件并在向导上显示下载链接。但我不想要这个额外的步骤。我希望通过单击下载文件。我在这里分享我的工作代码。请查看并建议我应该添加什么以使其只需单击即可工作。

xml代码:

            <div state="get">

                <group>

                    <field name="name" colspan="4" invisible="1"/>

                    <field name="report" filename="name" colspan="4"/>

                </group>

            </div>

            <button name="generate_xls_report" string="Export XLS" type="object" class="oe_highlight" />

Python 代码:

from odoo import fields, models, api, _

from odoo.tools import DEFAULT_SERVER_DATE_FORMAT
import xlwt
import base64
import cStringIO
from datetime import datetime


class CasesWizard(models.TransientModel):
    _name = "cases.wizard"
    _description = "Cases wizard"

    case_id = fields.Many2one('project.project', string='Cases')
    event_id = fields.Many2one('calendar.event', string='Events')
    company_id = fields.Many2one('res.company', string='company id', readonly=True,default=lambda self: self.env.user.company_id.id)
    lawyer_id = fields.Many2one('res.users', string='Lawyers')
    #partner_id = fields.Many2one('res.partner', string='Clients')
    date_from = fields.Date(string='Start Date')
    date_to = fields.Date(string='End Date')


    state = fields.Selection([('choose', 'choose'), ('get', 'get')],default='choose')
    report = fields.Binary('Prepared file', filters='.xls', readonly=True)
    name =  fields.Char('File Name', size=32)
    @api.multi
    def generate_xls_report(self):

        self.ensure_one()

        wb1 = xlwt.Workbook(encoding='utf-8')
        ws1 = wb1.add_sheet('Case Event Details')
        fp = cStringIO.StringIO()


# Here all excel data and calculations


        wb1.save(fp)
        out = base64.encodestring(fp.getvalue())
        self.write({'state': 'get', 'report': out, 'name':'event_details.xls'})

        return {
            'type': 'ir.actions.act_window',
            'res_model': 'cases.wizard',
            'view_mode': 'form',
            'view_type': 'form',
            'res_id': self.id,
            'views': [(False, 'form')],
            'target': 'new',
            'name': 'Event Details Report'
        }

【问题讨论】:

    标签: excel report odoo-10 generate


    【解决方案1】:

    您可以使用web controller 来实现这一点。
    在向导模型中添加以下方法:

    @api.multi
    def generate_xls_report(self):
        self.ensure_one()
        return {
            'type': 'ir.actions.act_url',
            'url': '/web/binary/download_xls_document?model=cases.wizard&id=%s&filename=event_details.xls' % (
                self.id),
            'target': 'new',
        }
    

    然后创建web控制器:

    class Binary(http.Controller):
    
    @http.route('/web/binary/download_xls_document', type='http', auth="public")
    @serialize_exception
    def download_xls_document(self, model, id, filename=None, **kw):
        Model = request.registry[model]
        cr, uid, context = request.cr, request.uid, request.context
    
        wb1 = xlwt.Workbook(encoding='utf-8')
        ws1 = wb1.add_sheet('Case Event Details')
        fp = cStringIO.StringIO()
    
        # Here all excel data and calculations
    
        wb1.save(fp)
        filecontent = fp.getvalue()
    
        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))])
    

    这应该会生成一个空的 xls 文件。

    【讨论】:

    • 亲爱的 Wako 我已按照您的上述解决方案原样执行并收到此错误:未找到服务器上未找到请求的 URL。如果您手动输入了 URL,请检查您的拼写并重试。
    • 还在错误日志中显示:NameError: name 'serialize_exception' is not defined。如果我要分享更多信息,请告诉我。
    • 亲爱的 WaKo 在使用代码一段时间后它正在工作。但是当下载 excel 文件时,我的向导无法正常工作。请告诉我如何避免我的向导不被宠坏或受到影响,以便我可以接受你的回答。如果您需要更多信息,请提前告诉我。
    • new替换目标中的self
    • 试过了,如果 self 被替换为 new 那么文件不会下载。当我将其保留为自我时,文件下载但向导上的所有按钮都被禁用。
    猜你喜欢
    • 2019-01-05
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多