如何使用Odoo 10中的向导生成Excel报告,只需单击一下即可

时间:2018-06-11 08:57:38

标签: excel report odoo-10 generate

我正在尝试使用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'
        }

1 个答案:

答案 0 :(得分:0)

您可以使用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文件。